【问题标题】:When document.ready doesn't mean document.ready当 document.ready 并不意味着 document.ready
【发布时间】:2011-05-04 15:21:26
【问题描述】:

我在 jQuery 选项卡 UI 中使用以下功能做几件事 - 根据数据属性更改图像,拉随机引用并折叠一些 div - 所有这些都在 jQuery UI 选项卡中的选项卡更改上。我还在第一个函数中通过 CSS 更改布局。

问题是文档就绪并不能真正起作用。如果我使用 #hash URL 从另一个页面返回到 0 选项卡,则会跳过第一个绑定事件并且 CSS 不会更改。

有没有办法让 document.ready 真正起作用?并使 CSS 更改保持一致?

$(document).ready(function () {

    $('#tabs').bind('tabsshow', function (event, ui) {

    var $tabs = $("#tabs").tabs();

        $('#wrapper').css('width', '586px'); //show certain CSS for the 0 tab
        $('#wrapper').css('margin', '80px auto');
        $('#col2, #footer, #headermain h1, .hide').css('display', 'none');

        if (ui.index != 0) {
            $('#wrapper').css('width', '875px'); //show certain CSS for all other tabs
            $('#wrapper').css('margin', '15px auto');
            $('#col2, #footer, #headermain h1, .hide').css('display', 'block');

        }
    });

    $(function () { //tab change fx
        $('#tabs').tabs({
            fx: {
                opacity: 'toggle'
            },


            select: function (event, ui) {
                $(".entry-content").hide('fast'); //collapse any open divs in other tabs on tab change
                $(".bkcontent").hide('fast');


                $('div#quotescontainer').load('quotes.html', function () {
                    var $quotes = $(this).find('div.quote'); //pull a random quote for the sidebar
                    var n = $quotes.length; //on any tab change
                    var random = Math.floor(Math.random() * n);
                    $quotes.hide().eq(random).fadeIn();
                });

                var img = $(ui.panel).data("image");
                $("#headerwrapper").animate({
                    opacity: 'toggle'
                }, function () { //change header image to the 
                    $(this).css("background-image", "url(" + img + ")") //data attr of the tabs div
                    .animate({
                        opacity: 'toggle'
                    });
                });
            }

        });
    });
});

【问题讨论】:

  • 我不确定您期望document.ready() 会做什么?请注意,它不会在页面的每次更新时触发,仅在页面第一次加载时触发。那是什么不工作?
  • @Pekka:jQuery 选项卡为每个选项卡提供了一个可收藏的 URL,例如“mydomain.com/mypage#my-tab-1”他在问为什么当他直接访问这个 URL 并且页面被加载时这不起作用。
  • @Pekka,是的,我需要在每次页面加载时触发它。特别是当我从 Wordpress 中的单个帖子页面转到“主要”URL 时,即 domain.com/2011/5/blahblah

标签: jquery jquery-ui-tabs document-ready


【解决方案1】:

http://bugs.jqueryui.com/ticket/3867

在调用.tabs()之前尝试绑定

编辑

那是因为您正在为这些功能使用“选择”事件。这意味着它们仅在单击选项卡时触发。您还希望它们在用户通过 URL 中的哈希直接进入选项卡时触发。

我认为使用show 而不是select 可能会让你走上正轨。

我也认为这样做可能更有意义:

var $tabs = $('#tabs').bind('tabsshow', function (event, ui) {

            $('#wrapper').css('width', '586px'); //show certain CSS for the 0 tab
            $('#wrapper').css('margin', '80px auto');
            $('#col2, #footer, #headermain h1, .hide').css('display', 'none');

            if (ui.index != 0) {
                $('#wrapper').css('width', '875px'); //show certain CSS for all other tabs
                $('#wrapper').css('margin', '15px auto');
                $('#col2, #footer, #headermain h1, .hide').css('display', 'block');

            }
        }).tabs({
            fx: {
                opacity: 'toggle'
            },


            show: function (event, ui) {
                $(".entry-content").hide('fast'); //collapse any open divs in other tabs on tab change
                $(".bkcontent").hide('fast');


                $('div#quotescontainer').load('quotes.html', function () {
                    var $quotes = $(this).find('div.quote'); //pull a random quote for the sidebar
                    var n = $quotes.length; //on any tab change
                    var random = Math.floor(Math.random() * n);
                    $quotes.hide().eq(random).fadeIn();
                });

                var img = $(ui.panel).data("image");
                $("#headerwrapper").animate({
                    opacity: 'toggle'
                }, function () { //change header image to the 
                    $(this).css("background-image", "url(" + img + ")") //data attr of the tabs div
                    .animate({
                        opacity: 'toggle'
                    });
                });
            }


    });

我的括号对吗? :)

无论如何,重要的是这些功能需要在标签内容显示时调用,而不仅仅是选择。如果show 不起作用,您可能需要编写一个函数来查找 url 哈希(意味着查看器正在直接打开一个选项卡)并根据它看到的内容触发其他函数。

【讨论】:

  • 非常感谢,这似乎是问题所在。让我试试看。
  • 啊,上面编辑的效果更好; CSS 更改现在重新加载。但是现在图像交换和引用拉取器不会每次都刷新;我仍然在做错事。这两个应该放在单独的 document.ready 函数中吗?
  • @songdogtech :我试图扩展我的答案以涵盖这些功能。请注意,我在技术上确实已经回答了您的原始问题;)
  • 谢谢;在函数末尾附近有一个额外的});。现在报价确实旋转了。但是,总是有一些东西:标题图像循环然后消失,我猜是通过显示:无?但是你得到了积分,谢谢。
猜你喜欢
  • 2016-04-05
  • 2012-03-28
  • 2012-08-28
  • 1970-01-01
  • 2018-05-12
  • 2013-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多