【问题标题】:How to select a specific class when others have the same name?当其他人同名时如何选择特定的类?
【发布时间】:2023-03-11 08:06:01
【问题描述】:

好的,假设我有一个如下所示的表格:

<table class="first-table" border="1" width="400px">
   <tr>
       <td><?php echo $row_cond['title']; ?></td>
       <td><a class="more" href="#" onClick="slideup()">Display More</a></td>
   </tr>
   <tr class="full_result">
       <td colspan="2"><?php echo $row_cond['description']; ?></td>
   </tr>
</table>

您可以看到我有一个链接,单击该链接将调用slideUp() 函数来显示或隐藏带有“full_result”类的tr:

var command = false;
function slideup() { //New function SlidUp
    if (command == false){
         $('.full_result').fadeIn();
         $('.more').text('Display More');
         command = true;
    }else{
         $('.full_result').hide();
         $('.more').text('Display Less');
         command = false;
    }
}

所以诀窍是,这是在一个 while 循环中并重复多次。那么我该如何做到这一点,以便当我点击链接时,我点击链接的表格中只显示类“full_result”。

当我点击任意表格中的按钮时,所有类同时显示和隐藏。

我的解决方案

$('.full_result').hide();
$(".more").on("click",function(){
    $('.more').html('Display More');
    $('.full_result').hide();
    $(this).parents("tr").next().next().fadeIn();
    $(this).html('');
    return false; //This stops the page jumping to the top of the page when I click the link
});

【问题讨论】:

  • 可能您可以考虑使用ID 而不是class 来满足您的要求。

标签: php javascript jquery while-loop show-hide


【解决方案1】:

尝试以这种方式更改您的 JS 代码并从“.more”链接中删除“onClick”属性:

var command = false;
$(".more").on("click",function(){
    if (command == false){
         $(this).parents("tr").next().fadeIn();
         $(this).html('Display Less');
         command = true;
    }else{
          $(this).parents("tr").next().hide();
         $(this).html('Display More');
         command = false;
    }
});

见jsfiddle

编辑

如果“.more”行下方有多行,您可以使用.nextAll() 显示/隐藏所有下一行。并且只需使用.slideToggle() 来避免使用“var command”。这段代码:

$(".more").on("click",function(){    
         $(this).parents("tr").nextAll().fadeToggle();
         if($(this).html()=="Display More") $(this).html('Display Less');            
         else $(this).html('Display More');
});

查看新的JSFiddle

【讨论】:

  • 嗨,这个似乎是所有这些中效果最好的,但它仍然不起作用。我应该说我使用actaully的表有3个部分,当我需要显示和隐藏第三个时,这段代码似乎显示和隐藏了第二个。我需要更改哪些内容才能使其正常工作?
  • 我认为您可能会在此解决方案中遇到command var 的问题。对于 .more 单击,它将设置为 true,但如果您在不关闭第一个实例的情况下单击另一个 .more,则 var 仍然为 true,并且不会发生任何事情。
  • 啊,我没注意到。有办法解决吗?
  • 在这种情况下尝试使用 .nextAll() 代替 .next()
  • 只是找到一种不同的方法来确定相关的.full_result 是否打开。多种方法来做到这一点。我在下面发布了一个解决方案。您还可以向元素添加一个“开放”类并检查它。
【解决方案2】:

你可以在 table 上有一个 common 类,并参考下面所需的 tr

1) 在表格标签上有一些通用类。例如:
2) 脚本应如下所示。这样你就不必调用内联函数了。

$(document).on('click', '.tr-hidable .more',function(){
  if (command == false){
    $(this).parents('tr-hidable').first().find('full-result').fadeIn();
    $(this).text('Display More');
    command = true;
  }else{
    $(this).parents('tr-hidable').first().find('full-result').fadeOut();
    $(this).text('Display Less');
    command = false;
  }
});

或者如果我们没有任何动态元素,我们可以将脚本的第一行替换为下面的行,所有的挖掘脚本将保持不变。

$('.tr-hidable .more').on('click',function(){

希望,它有帮助!

【讨论】:

  • 没有提到动态插入的元素。应该没有理由绑定到document,让tr 上的所有点击事件一直冒泡。 OP已经声明at the moment when I click the button in any table, all the classes show and hide at the same time这表明click事件的绑定已经正确,但执行的行为不是。
  • 我认为这是将事件绑定到文档的更好方法,这将使其也适用于动态插入的元素。其他方法也可以,但我认为它是更好的方法。
  • @perm:除非有动态元素,否则绑定到document 因为你may 有动态元素不是很实用。如果不需要,让所有事件一直冒泡到 document just-in-case 是非常低效的。 Only 如果动态元素需要附加事件,你会使用 on() 的动态绑定语法吗?即使那样,除非没有其他选择,否则应该绑定到最近的静态元素而不是 document。
  • @Francois:是的,明白了你的意思并更新了我的答案。谢谢你的信息。
【解决方案3】:

试试这个

HTML 代码

<table class="first-table" border="1" width="400px">
   <tr>
       <td><?php echo $row_cond['title']; ?></td>
       <td><a class="more" href="#">Display More</a></td>
   </tr>
   <tr class="full_result">
       <td colspan="2"><?php echo $row_cond['description']; ?></td>
   </tr>
</table>

JS 代码

$('.more').on('click', function(){
    slideup();

});
var command = true;
function slideup() { //New function SlidUp
    alert('ok');
    if (command == true){
         $('.full_result').fadeIn(400);
         $('.more').text('Display More');
         command = false;
    }else{
         $('.full_result').hide();
         $('.more').text('Display Less');
         command = true;
    }
}

【讨论】:

    【解决方案4】:

    您可以尝试以下方法:

    $('.more').on('click',function(){
        var text = $(this).text() === "Display More" ? "Display Less" : "Display More";
        $(this).text(text)
               .closest('table').children('.full_result').fadeToggle();
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-24
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2017-03-16
      • 2018-07-03
      相关资源
      最近更新 更多