【问题标题】:JQuery - Loading page into div with load() - How to Unload?JQuery - 使用 load() 将页面加载到 div - 如何卸载?
【发布时间】:2012-01-27 08:19:32
【问题描述】:

我有很多代码,并希望避免将它们全部发布在这里,但如果我需要,我会或者我会模拟类似的东西。

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

我希望这个描述是可以理解的。我猜我的问题可能很简单,并且与我尚未理解的层次结构有关。

谢谢!

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

这是加载页面中的 Javascript。它引用的所有类和 id 都存在于加载的页面中。 (代码改编自http://www.ilovecolors.com.ar/ 编码的标签行为)

    //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);
});

【问题讨论】:

  • div 中的 javascript 是否包含其外部元素的事件等?你能提供javascript吗?
  • 我在上面的描述中添加了 Javascript。

标签: jquery load jquery-load


【解决方案1】:

javascript每次都是一样的,只有加载页面的内容会改变(我假设内容会改变,否则每次都重新加载没有意义);因此,我会将脚本放在 div 中加载的文件之外,并在主页中仅加载一次。

【讨论】:

  • 每次重新加载 div 时选项卡的数量都会发生变化,因此在新页面加载时需要重新填充选项卡数组。我相信这是可能的,我只需要解决如何。
  • 但是您的评论以及我最近对代码的更改让我想知道为什么我仍然以这种方式加载页面。我仍然想知道为什么在加载新页面后 Javascript 会继续运行。Javascript 是否存在于不同的空间中?
  • 是的,这是可能的; jQuery .load() 接受回调函数作为第二个参数。将加载内容后要执行的代码放在那里
  • Javascript 与 DOM 内容不一样;它在加载的那一刻被执行。之后就无法卸载了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
  • 1970-01-01
相关资源
最近更新 更多