从jquery中的多个嵌套子元素中获取第一个可见文本

时间:2015-05-26 05:19:08

标签: jquery html text children elements

我无法解释我在文字中尝试做的事情,因为HTML代码可以与预期的输出一样清楚。但总结一下我尝试做的是使用jquery获取eBay拍卖的所有商品细节价值。对于Item条件,jquery代码甚至不输入div来检查文本,因为它没有在它之前找到任何文本。

我被建议使用这个无法正常运行的代码。

$("td").contents().filter(function(){ 
  return this.nodeType == Node.TEXT_NODE; 
})[0].nodeValue

无论如何,这里是3个项目细节值的HTML代码。

<td width="50.0%">
  <h2 itemprop="brand" itemscope="itemscope" itemtype="http://schema.org/Brand"><span itemprop="name">Unbranded</span></h2>
</td>

<td width="50.0%">
  <span>China</span>
</td>

<td width="50.0%">
  <!-- ITEM CONDITION  -->
  <!-- If Attribute is having hidden text / link     -->
  <div aria-live="polite">                                                  New: <span id="vi-cond-addl-info">A brand-new, unused, unopened, undamaged item in its original packaging (where packaging is </span>
  <span id="hiddenContent" class="u-dspn" aria-expanded="false" >
                                                                            applicable). Packaging should be the same as what is found in a retail store, unless the item is handmade or was packaged by the manufacturer in non-retail packaging, such as an unprinted box or plastic bag. See the seller's listing for full details.<a href="http://pages.ebay.com/help/sell/contextual/condition_1.html" target="_blank" class="infoLink u-nowrap" >
                                                                                    See all condition definitions<b class="g-hdn">- opens in a new window or tab</b></a>
                                                                            </span>

                                                                        <!-- TODO: remove hardcoded ID -->
                                                                        <span id="readFull" class="infoLink u-nowrap">
                                                                            ... <a href="javascript:;">Read more<b class="g-hdn">about the condition</b></a>    
                                                                        </span>
                                                                    </div>
                                                        <!-- </td> -->
</td>

输出必须是:

  1. 无品牌
  2. 中国
  3. 新(可能是新的:因为感觉不可能)
  4. 找到一个半工作的解决方案,无法弄清楚如何为每个td循环它。

    var first_line = $("#element")
                    .contents()
                     .filter(function() { 
                         return $.trim(this.innerHTML||this.data);
                     })
                      .first().text();
    

    这是我最好的尝试

    $('td').each(function() {
         alert(
           $(this).contents()
                  .filter(function() { 
                    return $.trim(this.innerHTML||this.data);
                  })
                  .first()
                  .text()
         );
    });
    

4 个答案:

答案 0 :(得分:2)

试试这个:

$('td').each (function() {
  var currenttd = $(this).children();
  var first_line = $(currenttd)
                   .contents()
                   .filter(function() { 
                       return !!$.trim( this.innerHTML || this.data ); 
                   })
                   .first();
  alert(first_line.text().trim());
});  

working fiddle

答案 1 :(得分:1)

这种作品:

var i = 0;
$('td').each(function () {
    if (i < 2) {
        alert($(this).contents().text());

    } else {
        alert($(this).text().split(':')[0]);
    }
    i++;
});

答案 2 :(得分:1)

使用此代码,

&#13;
&#13;
  $('table td').each(function() {
    var textVal = ($.trim($(this).text()).split(' '));
    alert(textVal[0]);
  });
&#13;
&#13;
&#13;

答案 3 :(得分:0)

使用#element部分抓取一组值,一次过滤一个:

var each_line = $("#element");
each_line.each(function() {
    $(this).contents()
                 .filter(function() { 
                     return $.trim(this.innerHTML||this.data);
                 })
                  .first().text();
});

编辑:

嗯...自己尝试New,然后寻找其余部分:

&#13;
&#13;
$('td').each(function() {
    var newItem = $.trim($('td > div').childNodes[0]);
     alert(
       $(this).contents()
              .filter(function() { 
                  return $.trim(this.innerHTML||this.data||newItem);
              })
              .first()
              .text()
     );
});
&#13;
<table>
<td width="50.0%">
  <h2 itemprop="brand" itemscope="itemscope" itemtype="http://schema.org/Brand"><span itemprop="name">Unbranded</span></h2>
</td>

<td width="50.0%">
  <span>China</span>
</td>

<td width="50.0%">
  <!-- ITEM CONDITION  -->
  <!-- If Attribute is having hidden text / link     -->
  <div aria-live="polite">                                                  New: <span id="vi-cond-addl-info">A brand-new, unused, unopened, undamaged item in its original packaging (where packaging is </span>
  <span id="hiddenContent" class="u-dspn" aria-expanded="false" >
                                                                            applicable). Packaging should be the same as what is found in a retail store, unless the item is handmade or was packaged by the manufacturer in non-retail packaging, such as an unprinted box or plastic bag. See the seller's listing for full details.<a href="http://pages.ebay.com/help/sell/contextual/condition_1.html" target="_blank" class="infoLink u-nowrap" >
                                                                                    See all condition definitions<b class="g-hdn">- opens in a new window or tab</b></a>
                                                                            </span>

                                                                        <!-- TODO: remove hardcoded ID -->
                                                                        <span id="readFull" class="infoLink u-nowrap">
                                                                            ... <a href="javascript:;">Read more<b class="g-hdn">about the condition</b></a>    
                                                                        </span>
                                                                    </div>
                                                        <!-- </td> -->
</td>
</table>
&#13;
&#13;
&#13;