【问题标题】:jquery get the descendent/child of a span tag in a DOMjquery获取DOM中span标签的后代/孩子
【发布时间】:2013-03-01 17:22:43
【问题描述】:

我在遍历一个DOM,需要获取span标签后面的文本

<span class="brand">Chanel</span>
- en cuir perle nacrée Wallet

我刚刚在 javascript 之后启动 jquery,想知道我需要使用什么选择器来选择 span 标签下方的文本?

谢谢

【问题讨论】:

  • 你能放更多的代码吗?到达那里的最佳方法是添加该文本的容器。
  • 基本上这是我想要达到的目标var productName = $('iframe').contents().find('.brand')[0].children().innerHTML;,但它并没有真正起作用,返回“不是方法”错误
  • 您是否尝试过接受的答案??

标签: jquery dom


【解决方案1】:
$('.brand').parent().contents().filter(function() {
    return this.nodeType == 3;
});

var txtElem = $('.brand').get(0).nextSibling;

FIDDLE

【讨论】:

    【解决方案2】:

    试试这个:- http://jsfiddle.net/5dxcG/

    $(function(){
        alert($(".brand").parent().text().replace($(".brand").text(),""));
    });
    

    【讨论】:

      【解决方案3】:
      $(function () {
          var texts = $('span.brand').map(function () {
              return this.nextSibling.nodeValue
          });
          alert(texts[0]);
      });
      

      Fiddle for this

      【讨论】:

        猜你喜欢
        • 2012-07-08
        • 1970-01-01
        • 1970-01-01
        • 2022-08-11
        • 1970-01-01
        • 2016-04-15
        • 1970-01-01
        • 2016-01-22
        • 1970-01-01
        相关资源
        最近更新 更多