使用jQuery更改div中的特定文本 [英] Change a specific text inside a div with jQuery

查看:460
本文介绍了使用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
    }
})

http://jsfiddle.net/DPVTg/

这篇关于使用jQuery更改div中的特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆