jquery - 仅定位部分 div 内容

标签 jquery

我有以下 html 标记

<div class="title-wrap">
      <span class="title">Hello World</span>
        <br/>
   This title is about Hello World
</div>

我想定位文本“This title is about Hello World”

但是我不知道如何在不包括跨度的情况下做到这一点?用这种格式的html可以实现吗?

我尝试过使用

var test = (jQuery('.title-wrap').text() || '' );
alert (test);

但这会返回

Hello World

This title is about Hello World

this fiddle所示.

最佳答案

您可以使用这个:

var test = (jQuery('.title-wrap')
        .clone()    //clone the element
        .children() //select all the children
        .remove()   //remove all the children
        .end()  //again go back to selected element
        .text() || '' );
alert (test);

输出:

This title is about Hello World

fiddle :http://jsfiddle.net/Y6dcU/6/

关于jquery - 仅定位部分 div 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12946768/

相关文章:

javascript - 如何使用 jQuery 更改仪表大小?

jquery - 在 knockout 事件中使用 jQuery 检索元素

asp.net - Javascript 文件依赖 - 选择性加载资源文件并防止重复

javascript - 在 Chrome 中切换选项卡后, Owl Carousel 停止

javascript - jQuery:将 img alt 标签更改为列表值

jquery - 不透明度悬停时图像上的文字

jquery 从对话框发布表单

jQuery 查询数据库

php - 我无法将值传递给 uploadify scriptData

javascript - 当条件匹配时使用 Jquery 定位多个元素