【问题标题】:jQuery using nextSibling to pull out datajQuery 使用 nextSibling 提取数据
【发布时间】:2012-02-13 00:40:45
【问题描述】:

我试图在下面的示例中只提取数字“88”:

    <li>
       <a href="website">5 stars</a>
       (88)
    </li>

我试过了:

var theLi= $('li');
var theNumber = $(theLi.get(0).nextSibling).text();

但什么都没有出现..它是空白的。

【问题讨论】:

    标签: jquery get nextsibling


    【解决方案1】:

    怎么样

    var theNumber = $('li').after( $('a') ).text().replace('(','').replace(')','')

    这行得通。

    【讨论】:

    • 请在发布之前使用 jsFiddle.net 测试您的答案,以免误导他人。您的方法无效:jsfiddle.net/Gq3nE
    • 非常接近,但括号仍然存在
    • @Todd 只需使用 replace: $('li').after( $('a') ).text().replace('(','').replace(')','')
    【解决方案2】:

    同样的事情已经在另一个帖子中得到了回答:Using .text() to retrieve only text not nested in child tags

    您克隆 li,删除子元素,然后提取文本。

    【讨论】:

    • 非常接近,但括号仍然存在。
    【解决方案3】:

    这应该可行,http://jsfiddle.net/elclanrs/bPqDR/:

    $('li').clone().children().remove().end().text();
    

    【讨论】:

    • 非常接近,但括号仍然存在
    • replace() 与正则表达式一起使用。
    【解决方案4】:

    您可以将lastChild 用于给定的结构。

    li.lastChild.nodeValue
    

    查看example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多