使用jQuery更改div中的特定文本
问题描述:
在下面的代码中,我想使用jQuery仅将"Going for"一词更改为"Highest bid:".我知道的唯一方法是使用replaceWith
来生成如下内容:$('#wp-bidcontainerleft').replaceWith('new word');
,但这将不起作用,因为我没有替换div中的所有内容.
In the code below, I would like to use jQuery to only change the words "Going for" to "Highest bid:". The only way I know how is to use replaceWith
to produce something like this: $('#wp-bidcontainerleft').replaceWith('new word');
but that wouldn't work since I'm not replacing everything within the div.
<div id="wp-bidcontainerleft">
Going for
<br>
$101.00
</div>
最简单的方法是什么?
答
您可以使用contents
方法:
$('#wp-bidcontainerleft').contents().each(function(i, v){
if (i === 0) {
this.textContent
? this.textContent = 'new word' // non-ie browsers
: this.innerText = 'new word'; // crazy ie
return false // stop executing each method
}
})