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 );
});
精彩评论