每个 if 类包含文本的 jQuery remove()

发布于 2024-11-07 13:14:09 字数 3078 浏览 0 评论 0原文

如果标题(类名称='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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咋地 2024-11-14 13:14:09

您需要使用函数中每个方法的当前上下文,而不是全局选择器

$('.bnum')

,考虑到基数与目标不在同一 tr 中,我们需要进行一些 Dom 遍历。

$(".colors_productname:contains('True')").each(function () {
    $(this).closest('tr').next('tr').find('.bnum').remove(); });

instead of a global selector

$('.bnum')

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.

$(".colors_productname:contains('True')").each(function () {
    $(this).closest('tr').next('tr').find('.bnum').remove(); });
掩饰不了的爱 2024-11-14 13:14:09

不应该是 $(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()

寄离 2024-11-14 13:14:09

试试这个:

$(document).ready(function() {
    $(".colors_productname:contains('True')").each(function () {
        $(this).find('.bnum').remove(); });
});  

Try this one instead:

$(document).ready(function() {
    $(".colors_productname:contains('True')").each(function () {
        $(this).find('.bnum').remove(); });
});  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文