获取不带输入标记的Td-innert html-Jquery

Get Td innert html without input tags - Jquery

本文关键字:Td-innert html-Jquery 输入 获取      更新时间:2024-04-26

我有下面这样的html:

<table>
  <tbody>
    <tr>
      <td>LG G5
        <input data-productid="da15d739" value="da15d739" name="PurchaseDetails[0].StoreDetails[2].ProductId" type="hidden">
        <input name="PurchaseDetails[0].StoreDetails.Index" value="146" type="hidden">
      </td>
    </tr>
  </tbody>
</table>

我只需要在Td中获得LG G5

如何使用Jquery获得它?

您可以使用.text()

$("td").text()将返回不带html标签的文本

您只需要获取文本节点:

 $('td').contents().filter(function() {
    return this.nodeType === Node.TEXT_NODE; 
 }).text();

请参阅此处的演示。

请找到以下代码:

HTML:

<table>
  <tbody>
    <tr>
      <td>LG G5
        <input data-productid="da15d739" value="da15d739" name="PurchaseDetails[0].StoreDetails[2].ProductId" type="hidden">
        <input name="PurchaseDetails[0].StoreDetails.Index" value="146" type="hidden">
      </td>
    </tr>
  </tbody>
</table>

JS:

$(function() {
  var data = $.trim($('td').text());
  console.log('sha', data);
});