简体   繁体   English

如何使用 jquery.load() 加载脚本?

[英]How to load script with jquery.load()?

I have a question.我有个问题。

In my file content.html I have script.在我的文件 content.html 中有脚本。 When load this with the load() function of jQuery, the page shows but the script doesn't work.当用jQuery的load()函数加载这个时,页面显示但脚本不起作用。 I think it's because of a selector.我认为这是因为选择器。

How can I solve that ?我该如何解决?

EDIT :编辑 :

$(document).ready(function(){
$('#content').load('content.html #toLoad', function(){});
})

Thanks谢谢

When you say "the script doesn't work" do you mean there's javascript in the page you're loading through ajax?当您说“脚本不起作用”时,您的意思是您通过 ajax 加载的页面中有 javascript 吗?

It's not going to work, ever, at least not directly.它永远不会起作用,至少不会直接起作用。

You have two choices.你有两个选择。 Parse out the script from the content you've loaded, and use eval to run it.从您加载的内容中解析出脚本,并使用eval运行它。

A better way is to use $.getScript to load (and execute) javascript.更好的方法是使用$.getScript加载(并执行)javascript。 If you want to bind your javascript with the HTML you've loaded, then you need some convention to identify the script.如果您想将您的 javascript 与您加载的 HTML 绑定,那么您需要一些约定来识别脚本。 For example you could include a tag in your dynamically loaded html:例如,您可以在动态加载的 html 中包含一个标签:

<span style="display:none;" id="script">/scripts/somescript.js</span>

Then look for a span with id="script" when you load the content, if you find it, use $.getScript to load the script it identifies.然后在加载内容时查找 id="script" 的 span,如果找到,则使用$.getScript加载它标识的脚本。

example...例子...

$.load('content.html #toLoad', function(data){
    var $script = $(data).find('span[id=script]');
    if ($script.length) {
        $.getScript($script.html());
        // remove the span tag -- no need to render it
        $script.remove();
    }
    $('#content').html(data);
});

I know this is an old question from several years ago, but I was not satisfied with the accepted answer when I stumbled onto this page.我知道这是几年前的一个老问题,但是当我偶然发现这个页面时,我对接受的答案并不满意。 Here's an alternative I wrote that automatically finds all of the scripts within the content you are loading, and runs each of those scripts regardless of whether the script is inline or an external file.这是我编写的替代方法,它会自动查找您正在加载的内容中的所有脚本,并运行这些脚本中的每一个,而不管脚本是内联的还是外部文件。

$("#content").load("content.html #toLoad", function(data) {
    var scripts = $(data).find("script");

    if (scripts.length) {
        $(scripts).each(function() {
            if ($(this).attr("src")) {
                $.getScript($(this).attr("src"));
            }
            else {
                eval($(this).html());
            }
        });
    }

});

This code finds all of the script elements in the content that is being loaded, and loops through each of these elements.此代码查找正在加载的内容中的所有脚本元素,并循环遍历这些元素中的每一个。 If the element has a src attribute, meaning it is a script from an external file, we use the jQuery.getScript method of fetching and running the script.如果元素有一个src属性,意味着它是来自外部文件的脚本,我们使用jQuery.getScript方法来获取和运行脚本。 If the element does not have a src attribute, meaning it is an inline script, we simply use eval to run the code.如果元素没有src属性,这意味着它是一个内联脚本,我们只需使用eval来运行代码。

I've tested this method in Chrome and it works.我已经在 Chrome 中测试了这种方法并且它有效。 Remember to be cautious when using eval , as it can run potentially unsafe scripts and is generally considered harmful.请记住在使用eval时要小心,因为它可能会运行潜在不安全的脚本并且通常被认为是有害的。 You might want to avoid using inline scripts when using this method in order to avoid having to use eval .您可能希望在使用此方法时避免使用内联脚本,以避免必须使用eval

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM