jquery切换问题

发布于 2024-10-08 19:48:05 字数 1664 浏览 0 评论 0原文

我正在尝试创建一个主切换功能,它将折叠/展开所有迷你切换。我的迷你切换功能是:

$('.tog').toggle(
   function(){ 
      $('#togStat').val(1);
      var myID = $(this).attr("id").split('-')[1];
      $('#collapseObj-'+myID).hide();
      $('#collapseImg-'+myID).attr({src:'images/collapse_tcat_collapsed.gif'});
   },
   function(){ 
      $('#togStat').val(0);
      var myID = $(this).attr("id").split('-')[1];
      $('#collapseObj-'+myID).show();
      $('#collapseImg-'+myID).attr({src:'images/collapse_tcat.gif'});
});

主功能是:

    <cfoutput>
      $('.togAll').toggle(
   function(){
      <cfloop from="1" to="10" index="i">
      $('##collapseObj-#i#').hide();
      $('##collapseImg-#i#').attr({src:'images/collapse_tcat_collapsed.gif'});
      </cfloop>
      $('##collapseImg-All').attr({src:'images/expand_icon.png'}); 
      $('##collapseImg-All').attr({title:'expand all'}); 
      $('##collapseImg-All').attr({alt:'expand all'}); 
   },
   function(){ 
      <cfloop from="1" to="#getMaxCatID.catID#" index="i">
      $('##collapseObj-#i#').show();
      $('##collapseImg-#i#').attr({src:'images/collapse_tcat.gif'});
      </cfloop>
      $('##collapseImg-All').attr({src:'images/collapse_icon.png'}); 
      $('##collapseImg-All').attr({title:'collapse all'});
      $('##collapseImg-All').attr({alt:'collapse all'});
      });
      </cfoutput>

我正在使用 Coldfusion。主函数循环从 1 到 X 并创建类似于以下内容的内容:

 $('#collapseObj-1').hide();
 $('#collapseObj-2').hide();
 $('#collapseObj-1').hide();

我的问题是当我单击主切换时,我必须双击迷你切换才能打开折叠的 div。有没有办法将切换(偶数,奇数)更改为切换(奇数,偶数)? 谢谢

I am trying to create a master toggle function that will collapse/expand all the mini toggles. my mini toggle function is:

$('.tog').toggle(
   function(){ 
      $('#togStat').val(1);
      var myID = $(this).attr("id").split('-')[1];
      $('#collapseObj-'+myID).hide();
      $('#collapseImg-'+myID).attr({src:'images/collapse_tcat_collapsed.gif'});
   },
   function(){ 
      $('#togStat').val(0);
      var myID = $(this).attr("id").split('-')[1];
      $('#collapseObj-'+myID).show();
      $('#collapseImg-'+myID).attr({src:'images/collapse_tcat.gif'});
});

and the master one is:

    <cfoutput>
      $('.togAll').toggle(
   function(){
      <cfloop from="1" to="10" index="i">
      $('##collapseObj-#i#').hide();
      $('##collapseImg-#i#').attr({src:'images/collapse_tcat_collapsed.gif'});
      </cfloop>
      $('##collapseImg-All').attr({src:'images/expand_icon.png'}); 
      $('##collapseImg-All').attr({title:'expand all'}); 
      $('##collapseImg-All').attr({alt:'expand all'}); 
   },
   function(){ 
      <cfloop from="1" to="#getMaxCatID.catID#" index="i">
      $('##collapseObj-#i#').show();
      $('##collapseImg-#i#').attr({src:'images/collapse_tcat.gif'});
      </cfloop>
      $('##collapseImg-All').attr({src:'images/collapse_icon.png'}); 
      $('##collapseImg-All').attr({title:'collapse all'});
      $('##collapseImg-All').attr({alt:'collapse all'});
      });
      </cfoutput>

i am using coldfusion. the master function loops through 1 to X and creates something similar to:

 $('#collapseObj-1').hide();
 $('#collapseObj-2').hide();
 $('#collapseObj-1').hide();

my problem is when i click on the master toggle, i have to double click on the mini toggles in order to open the collapsed divs. is there a way to change toggle(even,odd) to toggle(odd,even) ?
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

甜点 2024-10-15 19:48:05

这似乎与 onclick 操作相关:

   if (!$('#collapseObj-1').is(':hidden')) {
        $('#collapseObj-1').hide();
        $('#collapseImg-1').attr({src:'images/collapse_tcat.gif'});
     } else {
        $('#collapseObj-1').show();     
        $('#collapseImg-1').attr({src:'images/collapse_tcat_collapsed.gif'});
     }

this seems to work attached to an onclick action:

   if (!$('#collapseObj-1').is(':hidden')) {
        $('#collapseObj-1').hide();
        $('#collapseImg-1').attr({src:'images/collapse_tcat.gif'});
     } else {
        $('#collapseObj-1').show();     
        $('#collapseImg-1').attr({src:'images/collapse_tcat_collapsed.gif'});
     }

首先,jQuery.toggle 实际上切换可见性。您似乎将其视为具有 onShow 和 onHide 参数的函数,这是错误的。请参阅http://api.jquery.com/toggle/。按照您编写的方式,您有 2 个函数作为参数,但切换需要

  • 布尔值 showOrHide
  • int 持续时间、函数回调
  • int 持续时间、字符串缓动、函数回调

显然您对 .toggle() 的使用是错误的。如果您想在发生某个事件时更改可见性,您可以使用一个函数来编写它,该函数根据其当前的可见性显示/隐藏一个迷你切换。我看不到 #togStat 的作用,但我保留了它。

var togStat = $('#togStat');

function miniToggle(id) {
    var elem = $('#collapseObj-' + id);
    if (elem.is(':visible')) {
        togStat.val(1);
        elem.hide();
        $('#collapseImg-' + id).attr({src: 'images/collapse_tcat_collapsed.gif'});
    } else {
        togStat.val(0);
        elem.show();
        $('#collapseImg-' + id).attr({src: 'images/collapse_tcat.gif'});
    }
}

您可以将其称为元素编号(如您的示例中所示),如下所示:

miniToggle(1);

我希望这对您有帮助。如果没有,您可能需要发布一个示例页面,以便我们可以查看实际的代码。

First of all, jQuery.toggle actually toggles visibility. You seem to consider it like a function that has a onShow and onHide parameter, which is wrong. See http://api.jquery.com/toggle/. The way you wrote it, you have 2 functions as parameters, but toggle takes either

  • boolean showOrHide
  • int duration, function callback
  • int duration, string easing, function callback

Clearly your use of .toggle() is wrong. If you want to change the visibility when a certain event happens, you could write it with a function that shows/hides one mini toggle based on its current visibility. I cannot see what #togStat does, but I kept it in.

var togStat = $('#togStat');

function miniToggle(id) {
    var elem = $('#collapseObj-' + id);
    if (elem.is(':visible')) {
        togStat.val(1);
        elem.hide();
        $('#collapseImg-' + id).attr({src: 'images/collapse_tcat_collapsed.gif'});
    } else {
        togStat.val(0);
        elem.show();
        $('#collapseImg-' + id).attr({src: 'images/collapse_tcat.gif'});
    }
}

You could call this for an element number (like in your example) like this:

miniToggle(1);

I hope this helps you. If not, you might want to post an example page so we can check out the actual code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文