在容器中,我有文本和 img 标签,如何仅使用 js 或 jquery 获取文本


In a container I have text and img tag, how can I get only the text with js or jquery

我只需要获取此容器内的文本,但我对文本有<img>,我试了又试,但我不知道如何只获取文本。如果您看到下面的代码,我只想获取<th>内没有 <img> 标签的文本。这是代码:

<table>
    <thead>
        <tr>
            <th id="user_head_id">
                ID<img src="images/DownArrow16A.ico" class="order_arrows"></img>
            </th>
            <th id="user_head_name">
                User<img src="images/DownArrow16I.ico" class="order_arrows">
            </th>
            <th id="user_head_mail">
                E-mail<img src="images/DownArrow16A.ico" class="order_arrows">
            </th>
            <th id="user_head_nif">
                DNI<img src="images/DownArrow16A.ico" class="order_arrows">
            </th>
            <th id="user_head_permission">
                Permissions<img src="images/DownArrow16A.ico" class="order_arrows">
            </th>
            <th id="user_head_ban">
                Ban code<img src="images/DownArrow16A.ico" class="order_arrows">
            </th>
        </tr>
    </thead>
    <tbody id="seleccionable">
        <tr class="tr_users">
            <td class="user_id">'.$myarray[0].'</td>
            <td class="user_name">'.$myarray[1].'</td>
            <td class="user_mail">'.$myarray[2].'</td>
            <td class="user_nif">'.$myarray[3].'</td>
            <td class="user_permission">'.$myarray[4].'</td>
            <td class="user_ban">'.$myarray[5].'</td>';
        </tr>
    </tbody>
</table>

你可以这样做,切断字符串:

$('thead th').each(function(){
    var text = $(this).text().split('<img')[0];
    console.log(text);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <thead>
        <tr>
            <th id="user_head_id">
                ID<img src="images/DownArrow16A.ico" class="order_arrows"></img>
            </th>
            <th id="user_head_name">
                User<img src="images/DownArrow16I.ico" class="order_arrows">
            </th>
            <th id="user_head_mail">
                E-mail<img src="images/DownArrow16A.ico" class="order_arrows">
            </th>
            <th id="user_head_nif">
                DNI<img src="images/DownArrow16A.ico" class="order_arrows">
            </th>
            <th id="user_head_permission">
                Permissions<img src="images/DownArrow16A.ico" class="order_arrows">
            </th>
            <th id="user_head_ban">
                Ban code<img src="images/DownArrow16A.ico" class="order_arrows">
            </th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

但更好的方法是将文本放在pspan标签中并为其设置 id。

使用 text() 方法获取元素内的文本。text( ) 方法获取所有匹配元素的组合文本内容。

工作演示

$('th').each(function(){
  console.log($(this).text())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <thead>
        <tr>
            <th id="user_head_id">
                ID<img src="images/DownArrow16A.ico" class="order_arrows"></img>
            </th>
            <th id="user_head_name">
                User<img src="images/DownArrow16I.ico" class="order_arrows">
            </th>
            <th id="user_head_mail">
                E-mail<img src="images/DownArrow16A.ico" class="order_arrows">
            </th>
            <th id="user_head_nif">
                DNI<img src="images/DownArrow16A.ico" class="order_arrows">
            </th>
            <th id="user_head_permission">
                Permissions<img src="images/DownArrow16A.ico" class="order_arrows">
            </th>
            <th id="user_head_ban">
                Ban code<img src="images/DownArrow16A.ico" class="order_arrows">
            </th>
        </tr>
    </thead>
    <tbody id="seleccionable">
        <tr class="tr_users">
            <td class="user_id">'.$myarray[0].'</td>
            <td class="user_name">'.$myarray[1].'</td>
            <td class="user_mail">'.$myarray[2].'</td>
            <td class="user_nif">'.$myarray[3].'</td>
            <td class="user_permission">'.$myarray[4].'</td>
            <td class="user_ban">'.$myarray[5].'</td>';
        </tr>
    </tbody>
</table>