jquery历史插件不包括脚本?

时间:2011-11-28 16:53:13

标签: javascript jquery html

我正在使用jquery.history lugin:http://tkyk.github.com/jquery-history-plugin/

这个我将内容加载到div中。但是,脚本(在使用chrome webtools进行分析之后)不包含脚本。假设我有一个名为#content的div。如果我想将page.html加载到其中并且页面html包含javascript代码或包含(作为示例),请执行以下操作:

<script type="text/javascript" src="jquery.js"></script>    
<script type="text/javascript">
    alert("hello world");
</script>

它不会加载jquery.js或执行警报。在webtools中我反而看到它:

你明白了。没有。 所以它似乎以某种方式排除了javascript并且只挑选了html。在我的jqeury.history代码中,我只加载page.html的某个div(.target),它看起来像这样:

(function($){
        var origContent = "";

        function loadContent(hash) {

            $('#content').stop(true,true).fadeOut();
            if(hash != "") {

                if(origContent == "") {
                    origContent = $('#content').html();
                }
                $('#content').load(hash +".html .target",
                                   function(){  $(this).prettyPrint();  });
            } else if(origContent != "") {
                $('#content').html(origContent);
            }
            $('#content').stop(true,true).fadeIn("fast");
        }

        $(document).ready(function() {
                $.history.init(loadContent);

                $('#navigation a').not('.external-link').click(function(e) {

                        var url = $(this).data('name');
                        url = url.replace(/^.*#/, '');
                        $.history.load(url);
                        return false;

                    });
            });

    })(jQuery);

祝你好运!

1 个答案:

答案 0 :(得分:1)

jQuery“.load()”方法剥离<script>元素,并且当URL包含选择器时不运行它们。

            $('#content').load(hash +".html .target",
                               function(){  $(this).prettyPrint();  });

查看该URL如何在实际URL(以及分隔空间)后面有“.target”选择器?这就是触发这种行为的原因。

我前段时间记录了一个关于此问题的错误,结论是更新了文档。我认为转储脚本背后的原因是当“.load()”仅仅是一部分已加载的内容时,它无法确定脚本是否依赖于所选子集之外的其他脚本。

相关问题