【发布时间】: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