开发者

retrieve text value from an element with jQuery

开发者 https://www.devze.com 2023-02-14 12:00 出处:网络
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:

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

精彩评论

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