I have a number in a span tag. I need to get the value of the number in the span and then increment it inside jQuery. I know how to do this for a text input - can this be done to a span tag?
我在span标签中有一个数字。我需要获取span中数字的值,然后在jQuery中增加它。我知道如何为文本输入执行此操作 - 可以对span标记执行此操作吗?
SPAN tag
SPAN标签
<span class="changeNumber"> 33 </span>
jQuery span selector
jQuery span选择器
var commentNumAppend = $(this).closest(".songContainer").find(".changeNumber");
7 个解决方案
#1
30
Example -
示例 -
<span class="changeNumber">33</span>
$('.changeNumber').html(parseInt($('.changeNumber').html(), 10)+1)
#2
17
You can do something like:
你可以这样做:
var value = parseInt($(".changeNumber").text(), 10) + 1;
$(".changeNumber").text(value);
JsFiddle Example:
JsFiddle示例:
http://jsfiddle.net/stapiagutierrez/WXAvS/1/
http://jsfiddle.net/stapiagutierrez/WXAvS/1/
References:
参考文献:
parseInt()
- parseInt函数()
text()
- 文本()
#3
7
This is the most complete answer.
这是最完整的答案。
- This solution avoids selector repetition
- 该解决方案避免了选择器重复
- Handles the increment gracefully if the span does not have a value yet.
- 如果范围还没有值,则优雅地处理增量。
<span class="changeNumber"></span>
var $number = $('.changeNumber');
$number.html((parseInt($number.html(),10) || 0) + 1);
#4
2
Something like this:
像这样的东西:
var $span = $('#mySpanId');
$span.text(Number($span.text()) + 1);
http://jsfiddle.net/mattball/Cf834/
http://jsfiddle.net/mattball/Cf834/
#5
1
var spanVal=$("span").html();
if(!isNaN(spanVal)){
alert("value incremented"+ (++spanVal));
}
http://jsfiddle.net/c84F4/
#6
0
I assume you mean something like this?
我猜你的意思是这样的?
<span id="Nbr">123</span>
If so, the InnerHTML property will give you the content of the span
如果是这样,InnerHTML属性将为您提供跨度的内容
#7
0
$(".changeNumber").html(parseInt($(".changeNumber").html())+1)
#1
30
Example -
示例 -
<span class="changeNumber">33</span>
$('.changeNumber').html(parseInt($('.changeNumber').html(), 10)+1)
#2
17
You can do something like:
你可以这样做:
var value = parseInt($(".changeNumber").text(), 10) + 1;
$(".changeNumber").text(value);
JsFiddle Example:
JsFiddle示例:
http://jsfiddle.net/stapiagutierrez/WXAvS/1/
http://jsfiddle.net/stapiagutierrez/WXAvS/1/
References:
参考文献:
parseInt()
- parseInt函数()
text()
- 文本()
#3
7
This is the most complete answer.
这是最完整的答案。
- This solution avoids selector repetition
- 该解决方案避免了选择器重复
- Handles the increment gracefully if the span does not have a value yet.
- 如果范围还没有值,则优雅地处理增量。
<span class="changeNumber"></span>
var $number = $('.changeNumber');
$number.html((parseInt($number.html(),10) || 0) + 1);
#4
2
Something like this:
像这样的东西:
var $span = $('#mySpanId');
$span.text(Number($span.text()) + 1);
http://jsfiddle.net/mattball/Cf834/
http://jsfiddle.net/mattball/Cf834/
#5
1
var spanVal=$("span").html();
if(!isNaN(spanVal)){
alert("value incremented"+ (++spanVal));
}
http://jsfiddle.net/c84F4/
#6
0
I assume you mean something like this?
我猜你的意思是这样的?
<span id="Nbr">123</span>
If so, the InnerHTML property will give you the content of the span
如果是这样,InnerHTML属性将为您提供跨度的内容
#7
0
$(".changeNumber").html(parseInt($(".changeNumber").html())+1)