javascript - 在一个容器中我有文本和 img 标签,我怎样才能只用 js 或 jquery 获取文本

标签 javascript php jquery html html-table

我只需要获取此容器内的文本,但我有一个 <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。

关于javascript - 在一个容器中我有文本和 img 标签,我怎样才能只用 js 或 jquery 获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36864158/

相关文章:

javascript - 如何动态创建一个简单的json数组?

jquery - Nivo slider ,在触发之前,所有图像都在页面上可见

javascript - 添加代码智能到nodejs中的rest api链接

PHP MYSQL TWIG 下拉菜单创建

javascript - 在下拉列表中选择后隐藏或删除弹出的颜色框上的复选框

PHP `DateTime::days` 返回垃圾?

javascript - 如何在 jQuery 中引用一堆刚刚添加的列表项?

javascript - Jquery 仅附加到第一类元素

javascript - 使用 jquery 检查多个文本区域字段的值,然后执行函数

javascript - 获取完整数据集,使用带分页的 YUI 数据表排序