2012-01-27 241 views
3

我有很多的代码,并希望避免在这里发布,但如果我需要我会或我会嘲笑类似的东西。JQuery - 使用load()加载页面到div中 - 如何卸载?

我的问题是,我有一个隐藏的div,我正在加载一个页面(加载()),然后取消隐藏(与fadeIn())。当需要将这个div放开时,我会将一个空白页面加载到div中,然后淡出它。我已经通过向其中添加一个JavaScript警报来测试空白页面,并且确实正在加载,但是我加载到第一页中div的Javascript仍在运行。此Javascript现在是否属于父页面?有没有办法卸载它,使它不再运行或可用?我需要稍后再使用不同的动态内容加载页面,而仍在运行的JavaScript与再次加载时使用相同的Javascript冲突。然后再次。

我希望这个描述是可以理解的。我猜测我的问题可能非常简单,并且与我尚未理解的病友有关。

谢谢!

-------------编辑

这里是加载的页面中的Javascript。它所引用的所有类和标识都存在于加载的页面中。

//array to store IDs of our tabs 
var tabs = []; 
//index for array 
var ind = 0; 
//store setInterval reference 
var inter = 0; 

//change tab and highlight current tab title 
function change(stringref){ 
    //hide the other tabs 
    jQuery('.tab:not(#' + stringref + ')').hide(); 
    //show proper tab, catch IE6 bug 
    if (jQuery.browser.msie && jQuery.browser.version.substr(0,3) == "6.0") 
     jQuery('.tab#' + stringref).show(); 
    else 
     jQuery('.tab#' + stringref).fadeIn(200); 
    //clear highlight from previous tab title 
    jQuery('.htabs a:not(#' + stringref + 't)').removeClass('select'); 
    //highlight currenttab title 
    jQuery('.htabs a[href=#' + stringref + ']').addClass('select'); 
} 
function next(){ 
    alert("change"); 
    //call change to display next tab 
    change(tabs[ind++]); 
    //if it's the last tab, clear the index 
    if(ind >= tabs.length) 
     ind = 0; 
} 
jQuery(document).ready(function(){ 
    //store all tabs in array 
    jQuery(".tab").map(function(){ 
     tabs[ind++] = jQuery(this).attr("id"); 
    }) 
    //set index to next element to fade 
    ind = 1; 
    //initialize tabs, display the current tab 
    jQuery(".tab:not(:first)").hide(); 
    jQuery(".tab:first").show(); 
    //highlight the current tab title 
    jQuery('#' + tabs[0] + 't').addClass('select'); 
    //handler for clicking on tabs 
    jQuery(".htabs a").click(function(){ 

     //if tab is clicked, stop rotating 
     clearInterval(inter); 
     //store reference to clicked tab 
     stringref = jQuery(this).attr("href").split('#')[1]; 
     //display referenced tab 
     change(stringref); 
     return false; 
    }); 
    //start rotating tabs 
    inter = setInterval("next()", 3500); 
}); 
+0

div中的JavaScript是否包含事件等外部元素?你能提供的JavaScript? – 2012-01-27 08:21:25

+0

我已将Javascript加入到我上面的描述中。 – Imaginary 2012-01-27 08:27:55

回答

1

的JavaScript是相同的,每次也只有上加载页面的内容发生变化(代码由http://www.ilovecolors.com.ar/标识的标签行为改编)(我假设的内容发生变化或者重装它每次都不会做感);因此我会将该脚本放在div中加载的文件之外,并在主页面中加载一次。

+0

每次重新加载div时,标签的数量都会发生变化,所以当新页面加载时,标签数组需要重新填充。我确信这是可能的,我只需要弄清楚。 – Imaginary 2012-01-27 08:37:52

+0

但是,您的评论以及我的代码最近的一次更改让我想知道为什么我仍然以这种方式加载页面。我仍然想知道为什么JavaScript在加载新页面后继续运行。Javascript是否存在于不同的空间中? – Imaginary 2012-01-27 08:40:52

+0

是的,这是可能的; jQuery .load()接受一个回调函数作为第二个参数。把你想要执行的代码加载到那里后 – Wesley 2012-01-27 08:41:37