假设我需要在<p>
标记中增加一个数字字符。可以使用下面这样的东西吗?
$('p').text(parseInt("THIS") + 1);
当我执行以下操作时,this
似乎不是指同一个对象:
$('p').text($(this).height()); //prints 731
$('p').text($(this).text()); //changes to blank
答案 0 :(得分:3)
是的,但您必须使用匿名函数:
$('p').text(function(i, t) {
// i is the index of the element amongst the collection,
// t is the text if the element.
// assuming that the first text-character is a valid number:
return parseInt(t, 10) + 1;
});