在容器中,我有文本和 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>
但更好的方法是将文本放在p
或span
标签中并为其设置 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>
相关文章:
- 在容器中,我有文本和 img 标签,如何仅使用 js 或 jquery 获取文本
- jquery 图像旋转器(仅在 JS 中传递图像)
- 使JS对象从typeof=“object”变为typeof=“undefined”
- 使JS响应屏幕宽度
- 节点webkit仅运行.js文件
- 如何使js成为通过ajax提交的rails表单的格式
- 仅在JS中从LAT-LONG获取大陆
- 如何使 JS 函数重新转换为 HTML 表单
- JavaScript库安装与仅复制.js文件
- 使JS文件在浏览器调试窗口中不可查看
- 用户报告和涡轮链接 - 如何使JS涡轮链接兼容
- 如何仅将js文件引用到特定的脚本块
- 如何仅在 JS 变量为 true 时才显示图像
- 如何使 JS 将 12 小时的输入格式解析为 24 或
- 仅使用 JS 解析 JS
- 仅从 JS 中的文件中获取 YAML 文本
- 无法使JS函数正常工作
- 仅使用 JS 写入文本文件
- 如何使 JS 菜单在悬停或单击时打开
- 使用媒体查询在移动设备上仅包含 JS 文件