我只需要获取此容器内的文本,但我有一个 <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。
关于javascript - 在一个容器中我有文本和 img 标签,我怎样才能只用 js 或 jquery 获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36864158/