【问题标题】:Strange behaviour on jQuery functionjQuery函数的奇怪行为
【发布时间】:2010-03-10 21:21:50
【问题描述】:

为什么这两个函数有奇怪的行为,当我点击广告时,tbody 展开然后折叠。

第二个函数可以做一些测试,向 if、else 语句添加警报,但它只进入 if 语句,所以它只会滑动。

我注意到的另一件事是,当 tbody 中只有一行时,它可以正常工作。为什么?

我也尝试添加return false,什么都没有,它没有被调用两次或在任何其他地方的函数。

如果我能解决这个问题,我将不胜感激。

谢谢。

Function 1
$(function() {
    $("table.section thead").click(function() {
    $(this).next("table.section tbody").slideToggle("slow");

    });
});

Function 2
$(function() {
    $("table.section thead").click(function() {
      var body = $(this).next("table.section tbody");
      if (body.is(":visible"))
         body.slideUp("slow");
      else
         body.slideDown("slow");
    });
});

更新 表格

<table>
<thead>
<tr><td>heading></td></tr>
</thead>
<tbody>
<tr><td>content1</td></tr>
<tr><td>content2</td></tr>
</tbody>
</table>

【问题讨论】:

  • 这两个函数是同时绑定的,还是这两个尝试做同样的事情?
  • @Sorpigal - 这是他之前的问题的 2 次不同尝试。
  • 我只使用其中一种功能。这是我分别尝试的两个功能

标签: jquery function


【解决方案1】:

我查看了 jQuery 邮件列表,这是高度依赖于浏览器的,因为它们处理表格以及它们的阻塞方式完全不同。相反,我会尝试将头部放在不同的元素中并滑动整个表格,或者改为淡化身体,如下所示:

$(function() {
  $("table.section thead").click(function() {
    $(this).next("tbody").animate({opacity: 'toggle'});
  });
});

动态调整单个表格组件的尺寸并使其正确跨浏览器似乎不值得在 jQuery 框架中修复,所以我不希望这会很快改变。

【讨论】:

    【解决方案2】:

    目前尚不清楚,但如果您的脚本中同时包含这两个,则您将调用 .click() 两次,这相当于:

    $(function() { 
        $("table.section thead").click(function() { 
          $(this).next("table.section tbody").slideToggle("slow"); 
          var body = $(this).next("table.section tbody"); 
          if (body.is(":visible")) 
             body.slideUp("slow"); 
          else 
             body.slideDown("slow"); 
        }); 
    }); 
    

    【讨论】:

      猜你喜欢
      • 2014-05-24
      • 2015-11-05
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多