[英]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>
使用text()方法獲取元素內的text( )
。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>
您可以這樣做,剪切字符串:
$('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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.