每个 if 类包含文本的 jQuery remove()
如果标题(类名称='productnamecolor color_productname')包含文本“True”,则尝试删除“bnum”类中的内容。
基本上在下面的示例中,您不应该能够在“True Merchandiser 冰箱”下看到任何“Burkett Bucks”值,但它仍然应该在所有其他冰箱上:
<table width="300px">
<tr>
<td width="100%" valign="top"><a class="productnamecolor colors_productname" href=""> Star Griddle</a></td>
</tr>
<tr>
<td width="100%" valign="top"><table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td width="64%" valign="top"><font class="text colors_text rewardpoints"><span id="offers">Special Offers:</span><br>
<div class="bbucks"><img src="http://bit.ly/ihYMFL" id="bucks"> Earn <span class="bnum">2343</span> Burkett Bucks</div>
</font></td>
<td align="right" width="36%"></td>
</tr>
</tbody>
</table></td>
</tr>
<tr>
<td width="100%" valign="top"><a class="productnamecolor colors_productname" href=""> True Merchandiser Refrigerator</a></td>
</tr>
<tr>
<td width="100%" valign="top"><table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td width="64%" valign="top"><font class="text colors_text rewardpoints"><span id="offers">Special Offers:</span><br>
<div class="bbucks"><img src="http://bit.ly/ihYMFL" id="bucks"> Earn <span class="bnum">2343</span> Burkett Bucks</div>
</font></td>
<td align="right" width="36%"></td>
</tr>
</tbody>
</table></td>
</tr>
<tr>
<td width="100%" valign="top"><a class="productnamecolor colors_productname" href="">Generic Something</a></td>
</tr>
<tr>
<td width="100%" valign="top"><table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td width="64%" valign="top"><font class="text colors_text rewardpoints"><span id="offers">Special Offers:</span><br>
<div class="bbucks"><img src="http://bit.ly/ihYMFL" id="bucks"> Earn <span class="bnum">2343</span> Burkett Bucks</div>
</font></td>
<td align="right" width="36%"></td>
</tr>
</tbody>
</table></td>
</tr>
</table>
我遇到的问题是它正在为所有值执行此操作,我只想删除每个标题下包含“True”一词的值。所以我假设我的每个都有问题。
$(document).ready(function() {
$(".colors_productname:contains('True')").each(function () {
$('.bnum').remove(); });
});
Trying to remove what is in the class 'bnum' IF the title (class name='productnamecolor colors_productname') contains the text 'True'.
Basically in the below example you SHOULD NOT be able to see any "Burkett Bucks" value under the 'True Merchandiser Refrigerator', however it should still be on all the others:
<table width="300px">
<tr>
<td width="100%" valign="top"><a class="productnamecolor colors_productname" href=""> Star Griddle</a></td>
</tr>
<tr>
<td width="100%" valign="top"><table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td width="64%" valign="top"><font class="text colors_text rewardpoints"><span id="offers">Special Offers:</span><br>
<div class="bbucks"><img src="http://bit.ly/ihYMFL" id="bucks"> Earn <span class="bnum">2343</span> Burkett Bucks</div>
</font></td>
<td align="right" width="36%"></td>
</tr>
</tbody>
</table></td>
</tr>
<tr>
<td width="100%" valign="top"><a class="productnamecolor colors_productname" href=""> True Merchandiser Refrigerator</a></td>
</tr>
<tr>
<td width="100%" valign="top"><table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td width="64%" valign="top"><font class="text colors_text rewardpoints"><span id="offers">Special Offers:</span><br>
<div class="bbucks"><img src="http://bit.ly/ihYMFL" id="bucks"> Earn <span class="bnum">2343</span> Burkett Bucks</div>
</font></td>
<td align="right" width="36%"></td>
</tr>
</tbody>
</table></td>
</tr>
<tr>
<td width="100%" valign="top"><a class="productnamecolor colors_productname" href="">Generic Something</a></td>
</tr>
<tr>
<td width="100%" valign="top"><table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td width="64%" valign="top"><font class="text colors_text rewardpoints"><span id="offers">Special Offers:</span><br>
<div class="bbucks"><img src="http://bit.ly/ihYMFL" id="bucks"> Earn <span class="bnum">2343</span> Burkett Bucks</div>
</font></td>
<td align="right" width="36%"></td>
</tr>
</tbody>
</table></td>
</tr>
</table>
Problem I'm having is that it is doing it for all values, I only want to remove the value under EACH title that has the word 'True' in it. So im assuming something wrong with my EACH.
$(document).ready(function() {
$(".colors_productname:contains('True')").each(function () {
$('.bnum').remove(); });
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用函数中每个方法的当前上下文,而不是全局选择器
,考虑到基数与目标不在同一 tr 中,我们需要进行一些 Dom 遍历。
instead of a global selector
you need to use the current context of the each method in your function, taking into account that the base is not in the same tr as the target we will need do a little Dom traversal.
不应该是
$(this).find('.bnum').remove()
或更短:$(".colors_productname:contains('True') .bnum").remove()
shouldn't it be
$(this).find('.bnum').remove()
or even shorter:$(".colors_productname:contains('True') .bnum").remove()
试试这个:
Try this one instead: