jQuery只在单击选项卡时加载页面一次
我有一个页面,上面有一些选项卡,单击某些选项卡时,其他页面会动态加载。唯一的问题是每次单击选项卡时都会加载这些页面。如何使它们在第一次单击时仅加载一次 我使用的代码如下:jQuery只在单击选项卡时加载页面一次,jquery,jquery-tabs,jquery-load,Jquery,Jquery Tabs,Jquery Load,我有一个页面,上面有一些选项卡,单击某些选项卡时,其他页面会动态加载。唯一的问题是每次单击选项卡时都会加载这些页面。如何使它们在第一次单击时仅加载一次 我使用的代码如下: $(document).ready(function(){ $(".tab-content").hide(); $("#one").show(); $("a.tab-name").click(function(){ $(".tab-name-active").removeClass("
$(document).ready(function(){
$(".tab-content").hide();
$("#one").show();
$("a.tab-name").click(function(){
$(".tab-name-active").removeClass("tab-name-active");
$(this).addClass("tab-name-active");
$(".tab-content").fadeOut();
var content_show=$(this).attr("title");
if (content_show === 'three') {
$("#"+content_show).load('new-page.php', function() {
$("#sorttable").tablesorter({
headers: {0: {sorter: false}}
});
});
}
else if (content_show === 'four') {
$("#"+content_show).load('new-page-2.php');
}
$("#"+content_show).delay(100).fadeIn('slow');
return false;
});
});
谢谢 我昨天刚做了一个这样的特写。我这样做的方式是在任意div(比如容器div)中添加一个“仍在加载”类,然后当选项卡最终加载时,再次删除该类。使用
.data()
保存布尔值
$(document).ready(function(){
$(".tab-content").hide();
$("#one").show();
$("a.tab-name").data('loaded',false).click(function(){
var $this = $(this);
$(".tab-name-active").removeClass("tab-name-active");
$this.addClass("tab-name-active");
$(".tab-content").fadeOut();
var content_show= this.title;
if (! $this.data('loaded')) {
if (content_show === 'three') {
$("#"+content_show).load('new-page.php', function() {
$("#sorttable").tablesorter({
headers: {0: {sorter: false}}
});
$this.data('loaded',true);
});
}
else if (content_show === 'four') {
$("#"+content_show).load('new-page-2.php', function(){
$this.data('loaded',true);
});
}
}
$("#"+content_show).delay(100).fadeIn('slow');
return false;
});
});