找到删除 JQuery Accordion 时容器的长度?

发布于 2024-12-01 08:24:00 字数 975 浏览 2 评论 0原文

我有以下代码来删除动态生成的 ui-accordion。当删除发生时(有效),我需要查明作为 div 的手风琴容器是否为空。因此,如果用户删除手风琴并且容器 div 现在为空,我需要调整包含的 div 的大小(该 div 是 ui.layout 窗格,当容器为空时,我需要自动为用户调整大小)。

当我删除手风琴时,长度仍然是1。当它是0时,我需要调整大小。这可能吗?

$( ".delete_accordion" )
        .click(function() {
            var parent = $(this).closest('h3');
            var head = parent.next('div');
            parent.add(head).fadeOut('slow',function(){$(this).remove();});    

            //length always returns 1 in the function even though it has been removed above.
            //perhaps there is an event or method I can intercept on delete?

            var parentHasClass = parent.hasClass("accordion");   

            var isempty = ($("#accordion").length == 0);
            alert(isempty);

            if(parentHasClass == "true"){                   
                if( isempty ){
                 myLayout.sizePane("west", 100);
                }                
            }

        });

I have the following code to delete a dynamically generated ui-accordion. When the delete occurs (which works) I need to find out if the accordion container which is a div is empty. So if the user deletes the accordion and the container div is now empty I need to resize the containing div (the div is ui.layout pane which I need to resize automatically for the user when the conainter is empty).

When I delete the accorion the length is still 1. I need to resize when it is 0. Is this possible?

$( ".delete_accordion" )
        .click(function() {
            var parent = $(this).closest('h3');
            var head = parent.next('div');
            parent.add(head).fadeOut('slow',function(){$(this).remove();});    

            //length always returns 1 in the function even though it has been removed above.
            //perhaps there is an event or method I can intercept on delete?

            var parentHasClass = parent.hasClass("accordion");   

            var isempty = ($("#accordion").length == 0);
            alert(isempty);

            if(parentHasClass == "true"){                   
                if( isempty ){
                 myLayout.sizePane("west", 100);
                }                
            }

        });

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

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

发布评论

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

评论(1

甜嗑 2024-12-08 08:24:00

$(this).remove() 直到 fadeOut 完成后才会被调用,因此从技术上讲,长度仍然 === 1。

您可以通过将其余的删除逻辑放入淡出回调:

  $( ".delete_accordion" )
          .click(function() {
              var parent = $(this).closest('h3');
              var head = parent.next('div');
              parent.add(head).fadeOut('slow',function(){
                $(this).remove();
                var parentHasClass = parent.hasClass("accordion");   

                var isempty = ($("#accordion").length == 0);
                alert(isempty);

                if(parentHasClass == "true"){                   
                    if( isempty ){
                     myLayout.sizePane("west", 100);
                    }                
                }
                });    

          });   

//I modified the selector as follows and now works great! Thanks Skylar!
$( ".delete_accordion" )
          .click(function() {
              var parent = $(this).closest('h3');
              var head = parent.next('div');
              parent.add(head).fadeOut('slow',function(){
                $(this).remove();
                var parentHasClass = parent.hasClass("accordion");  
                var isempty = ($("#accordion h3").length);
                if(isempty == 0 || parentHasClass == "true"){  
                     //alert(isempty);                 
                     myLayout.sizePane("west", 100);
                }
                });   

        });     

The $(this).remove() is not called until after the fadeOut completes, so technically the length still does === 1.

You can fix this by putting the rest of your delete logic in the fadeOut callback:

  $( ".delete_accordion" )
          .click(function() {
              var parent = $(this).closest('h3');
              var head = parent.next('div');
              parent.add(head).fadeOut('slow',function(){
                $(this).remove();
                var parentHasClass = parent.hasClass("accordion");   

                var isempty = ($("#accordion").length == 0);
                alert(isempty);

                if(parentHasClass == "true"){                   
                    if( isempty ){
                     myLayout.sizePane("west", 100);
                    }                
                }
                });    

          });   

//I modified the selector as follows and now works great! Thanks Skylar!
$( ".delete_accordion" )
          .click(function() {
              var parent = $(this).closest('h3');
              var head = parent.next('div');
              parent.add(head).fadeOut('slow',function(){
                $(this).remove();
                var parentHasClass = parent.hasClass("accordion");  
                var isempty = ($("#accordion h3").length);
                if(isempty == 0 || parentHasClass == "true"){  
                     //alert(isempty);                 
                     myLayout.sizePane("west", 100);
                }
                });   

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