【问题标题】:Selecting text without an ID or class using jQuery selectors使用 jQuery 选择器选择没有 ID 或类的文本
【发布时间】:2013-03-04 01:34:33
【问题描述】:

使用以下模板,我怎样才能得到“获取此文本”

<h1 class="classA classB">
        Obtain this text 
        <span class="unwanted">
            unwanted text
        </span>
</h1>

我尝试了$('.classA.classB:not(.unwanted)'),但我得到了整件东西而没有多余的东西。

【问题讨论】:

    标签: jquery dom jquery-selectors


    【解决方案1】:
    $("#foo")
        .clone()    //clone the element
        .children() //select all the children
        .remove()   //remove all the children
        .end()  //again go back to selected element
        .text();    //get the text of element
    

    .remove() 接受一个选择器,所以你可以做类似.remove('.unwanted')

    http://viralpatel.net/blogs/jquery-get-text-element-without-child-element/

    演示

    http://viralpatel.net/blogs/demo/jquery/get-text-without-child-element/

    询问前请先谷歌

    【讨论】:

      【解决方案2】:

      jsFiddle
      克隆它,放下孩子,获取文本:

      $('.classA').clone().find('.unwanted').remove().end().text();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多