jQuery, get and set tag values
I have a
<span id="test">5<开发者_StackOverflow社区/span>
and I wand to get its value, 5. And I want to change that value. how do I realize that.
Get it:
$("#test").text();
Set it:
$("#test").text("New text");
Reference: jQuery API
$('#test').text(); // Gets
$('#test').text('hello'); // Sets
Took about 10 seconds to get into the documentation http://jquery.com and search for "text" and read it...
To get it, you probably want the number, so get the .text()
and parse it with parseInt()
, like this:
var val = parseInt($("#test").text(), 10);
To set it just pass the value to .text()
, like this:
$("#test").text(newVal);
You cant test it out here.
精彩评论