开发者

retrieve text value from an element with jQuery

If I have an HTML element with a value in it:

<span id="mySpan">23</span>

How can I retrieve this value (23) and u开发者_StackOverflow中文版se it? I've tried something like:

var num = $("#mySpan").text();

Does not seem to work... Will I be able to use it as a number to add other numbers to it?


You want parseInt. You should also make sure that the selector is returning the DOM elements you expect it to.

var num = parseInt($("#myspan").text(), 10);


Make sure the DOM is loaded before your code runs:

$(function() {
    var num = +$("#mySpan").text();
    alert( num );
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜