【问题标题】:Script Outside of $(document).ready$(document).ready 之外的脚本
【发布时间】:2011-05-23 16:00:01
【问题描述】:

我已经为客户建立了一个网站,当用户点击导航链接时,链接页面的内容会动态加载并使用 JQuery 进行转换,而不是加载新页面。

我遇到的问题是,因为它没有加载新页面,所以 $(document).ready 不会再次触发,并且各个页面上的任何 JS 都会损坏。例如,如果您访问http://www.woodlandexotica.com/species.php,页面可以正常工作,但如果您尝试从http://www.woodlandexotica.com/index_dev.php 导航到该页面,则 JS 将无法正常工作。

我不是 JS 方面的专家,我非常感谢任何和所有帮助!

【问题讨论】:

  • 究竟是什么不工作?看起来精致而时尚。我的意思是:“物种”页面加载并显示。你的那个页面不应该有 JS,只有内容,所有的 JS 都在你的 php 主页面,你不会有任何问题。
  • 您还没有发布加载新页面的代码。到目前为止,关于您的页面需要注意的一件事是,您似乎在重复使用“id”值。这是一个非常糟糕的主意。每个“id”在整个页面上都必须是唯一的
  • @user 对我来说很好,您应该只在 document.ready 中绑定所有实时点击或鼠标悬停,所有其他逻辑应该在 documentready 之外,这样它们将是全局的并且不会创建任何范围问题
  • 例如,如果您访问woodlandexotica.com/species.php,您会看到不同的物种是隐藏的,只有在您点击名称时才会显示,但如果您访问woodlandexotica.com/index_dev.php 并导航物种页面,隐藏非活动DIV的JS不起作用。
  • @kobe 在加载时引用 DOM 元素的任何内容(即,在加载/执行脚本时,它引用 DOM 元素)都需要在 document.ready 中。

标签: javascript jquery document-ready


【解决方案1】:

问题在于,当您调用“.load()”时,您使用的是 URL 字符串和选择器后缀从加载的内容中提取。当你以这种方式使用“.load()”时,jQuery 会删除所有脚本并且不会运行它们。除了实现你自己版本的“.load( )" 您自己,或者(更好地)让您加载的其他页面不是是完整的 HTML 页面。如果您在 URL 字符串上使用没有选择器后缀的“.load()”,则 jQuery 运行脚本。

请参阅jQuery bug 6307 了解更多信息。该错误不会被修复,但希望文档会得到改进。

【讨论】:

    【解决方案2】:

    你组织这段代码的方式是错误的

    只保留绑定的内部 document.ready 并将逻辑移到外部的函数中。任何页面都可以访问。

    $(document).ready(function() {
    
    
        //////////////////////////////////////////////////
        //////////////////////////////////////////////////
    
        // CONTENT BG SLIDESHOW
    
        //////////////////////////////////////////////////
        var photos = ["images/bg01.jpg", "images/bg02.jpg", "images/bg03.jpg"];
    
        var slideshowSpeed = 8000;
    
        var interval;   
        var activeContainer = 1;    
        var currentImg = 0;
        var navigate = function(direction) {
            currentImg++;
            if(currentImg == photos.length + 1) {
                currentImg = 1;
            }
    
            // Check which container we need to use
            var currentContainer = activeContainer;
            if(activeContainer == 1) {
                activeContainer = 2;
            } else {
                activeContainer = 1;
            }
    
            showImage(photos[currentImg - 1], currentContainer, activeContainer);       
        };
    
        var currentZindex = 1;
        var showImage = function(photoObject, currentContainer, activeContainer) {
            // Make sure the new container is always on the background
            currentZindex--;
    
            // Set the background image of the new active container
            $("#bgimg" + activeContainer).css({
                "background-image" : "url(" + photoObject + ")",
                "display" : "block",
                "z-index" : currentZindex
            });
    
            // Fade out the current container
            // and display the header text when animation is complete
            $("#bgimg" + currentContainer).fadeOut(function() {
                setTimeout(function() {
                    animating = false;
                }, 500);
            });
            $("#bgimg" + currentContainer).css({
                "z-index" : "1"
            });
            currentZindex = 1;
        };
    
        function photoLoaded() {
            if(!--numPhotosLeft) {
                navigate("next");
    
                interval = setInterval(function() {
                    navigate("next");
                }, slideshowSpeed);
    
                $('#bg_load').fadeOut('fast');
                $('#page_bg').animate({opacity: 1, marginLeft: '-=860'}, 500);
            }
        }
    
        var photos = ["images/bg01.jpg", "images/bg02.jpg", "images/bg03.jpg"];
        var numPhotosLeft = photos.length;
    
        for(var i = 0; i < photos.length; ++i) {
            var img = new Image();
            img.onload = photoLoaded;
            img.src = photos[i];
        }
        //////////////////////////////////////////////////
        //////////////////////////////////////////////////
    
    
    
    
        //////////////////////////////////////////////////
        //////////////////////////////////////////////////
    
        // PAGE TRANSITION
    
        //////////////////////////////////////////////////
    
    
        // ADJUST FOR DEEPLINKING
        var hash = window.location.hash.substr(1);
        var href = $('a.link').each(function(){
            var href = $(this).attr('href');
            if(hash==href.substr(0,href.length-4)){
                var toLoad = hash+'.php #page_bg';
                $('#page_bg').load(toLoad)
            }                                           
        });
    
        $('a.link').click(function() {
    
            var toLoad = $(this).attr('href')+' #page_bg';
            $('#page_bg').animate({opacity: 0.25, marginLeft: '-=875'}, 500, loadContent);
    
            window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-4); //MODIFY FOR DEEP LINKING
    
            function loadContent() {
                $('#page_wrap').prepend('<span id="load">LOADING...</span>');
                $('#load').fadeIn('fast');
    
                $('#page_bg').css('marginLeft', 860);
                $('#page_bg').css('backgroundImage', 'none');
                $('#page_bg').load(toLoad,'',hideLoader);
            }
            function hideLoader() {
                $('#load').fadeOut('fast', showNewContent);
            }
            function showNewContent() {         
                $('#page_bg').animate({opacity: 1, marginLeft: '-=860'}, 500);
            }
    
            return false;
        });
    
        //set initial position and opacity
        $('#page_bg').css('marginLeft', 860);
        $('#page_bg').css('opacity', 0.25);
        $('#page_wrap').prepend('<span id="bg_load">LOADING...</span>');
        $('#bg_load').fadeIn('fast');
    
        //////////////////////////////////////////////////
        //////////////////////////////////////////////////
    
    });
    

    【讨论】:

      猜你喜欢
      • 2011-12-19
      • 1970-01-01
      • 2011-03-22
      • 1970-01-01
      • 2014-04-20
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      相关资源
      最近更新 更多