开发者

jQuery, get and set tag values

开发者 https://www.devze.com 2023-01-16 16:06 出处:网络
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:

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.

0

精彩评论

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