jQuery:选择具有特定 id 的跨度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12307234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: selecting a span with a particular id
提问by Leahcim
I'm getting an error message on this function telling me that parseInt doesn't have a radix parameter, even though I'm passing it the variable moveCount. I assume this means that the value of the span with the id #moveCount is not being assigned to the variable moveCount. I've marked the line below where I think the problem's arising. I've tried this line
我收到有关此函数的错误消息,告诉我 parseInt 没有基数参数,即使我将变量 moveCount 传递给它。我认为这意味着 id 为 #moveCount 的跨度值没有分配给变量 moveCount。我在下面我认为出现问题的地方做了标记。我试过这条线
moveCount = $('span #moveCount').html();
both with and without using the word 'span' in the selector and am getting the same result.
在选择器中使用和不使用“span”这个词,得到的结果是一样的。
function incrementMoveCount() {
//gets the html of the span with id
//moveCount
//turns it into a number
//increments it by one
//sets the html of the span with id moveCount
//to the new move count
var moveCount = 0;
var newMoveCount = 0;
moveCount = $('span #moveCount').html(); # problem here
newMoveCount = parseInt(moveCount);
newMoveCount = newMoveCount + 1;
$('span #moveCount').html('newMoveCount');
}
html
html
<div id="meta">
<h1>Checkers</h1>
<h4>Moves: <span id="moveCount">0</span></h4>
</div>
回答by ?????
You can remove the space if it's the span's id - space specifies descendants.. so you are actually looking for descendants of span with an id=moveCount
如果它是跨度的 id,您可以删除空格 - 空间指定后代..所以您实际上是在寻找具有 id=moveCount 的跨度的后代
$('span#moveCount').html();
Or since you are selecting by ID you don't need to specify span as ID selector is the fastest - ID's are unique so it will always look for the first element with that id
或者,由于您是按 ID 选择的,因此您不需要指定跨度,因为 ID 选择器是最快的 - ID 是唯一的,因此它将始终查找具有该 ID 的第一个元素
$('#moveCount').html();
回答by Billy Moon
Try removing the space...
尝试删除空格...
moveCount = $('span#moveCount').html();
Your selector is selecting an element with an id #moveCount
withina span
element
您的选择与一个id选择一个元素#moveCount
中的一个span
元素
回答by Kevin B
There can only be one element with the given id, you don't need to specify that it is a span.
给定的 id 只能有一个元素,你不需要指定它是一个跨度。
$("#moveCount").html()
If that doesn't give you what you are looking for, the problem isn't in the jQuery code and we need to see html.
如果这没有给你你想要的东西,问题不在 jQuery 代码中,我们需要查看 html。
Edit:
编辑:
$('#moveCount').html('newMoveCount');
should be
应该
$('#moveCount').html(newMoveCount);
回答by amitsaurav
If you are getting an element by id, you don't need the tag name to search it. Since, an HTML document should theoretically have only one element with a particular id. Thus in your case: $('#moveCount'); should also get you the element. I would change the code to be:
如果您通过 id 获取元素,则不需要标签名称来搜索它。因为,一个 HTML 文档理论上应该只有一个具有特定 id 的元素。因此,在您的情况下: $('#moveCount'); 还应该为您提供元素。我会将代码更改为:
var moveCount = + $('#moveCount').text();
$('#moveCount').text(moveCount + 1);
The jquery .text() method will correctly get the value of the span and the '+' sign in front will automatically convert it to int. You don't need to call parseInt(). We can speeden it up further by firing the jQuery select just once:
jquery .text() 方法会正确获取span 的值,前面的“+”号会自动将其转换为int。您不需要调用 parseInt()。我们可以通过只触发一次 jQuery select 来进一步加快速度:
var moveCountElement = $('#moveCount');
moveCountElement.text(+ moveCountElement.text() + 1);
Hope that helps!
希望有帮助!
回答by SNAG
.html('newMoveCount');
means that the html of the span should be the string 'newMoveCount' not the value of newMoveCount variable. also there should be no space in between span and the id. All in all, this should work:
意味着跨度的 html 应该是字符串 'newMoveCount' 而不是 newMoveCount 变量的值。span 和 id 之间也不应该有空格。总而言之,这应该有效:
function incrementMoveCount() {
var moveCount = 0;
var newMoveCount = 0;
moveCount = $('span#moveCount').html();
newMoveCount = parseInt(moveCount);
newMoveCount = newMoveCount + 1;
$('span#moveCount').html(newMoveCount);
}