简体   繁体   English

通过jQuery .load加载后执行jQuery脚本

[英]Execute jQuery script after its being loaded in via jQuery .load

In page Index.html there is a selectbox called #choose_content_to_load and a div called #get_loaded_content 在Index.html页面中,有一个名为#choose_content_to_load和一个名为#get_loaded_content的div

<select id="choose_content_to_load">
<option>Some content from page content.html and div #content</option>
</select

<div id="get_loaded_content">

As seen in the selectbox option there is a page called content.html that contains a div called #content . 如selectbox选项所示,存在一个名为content.html的页面,其中包含一个名为#content的div。 In the current situation the div #content get loaded into #get_loaded_content when click on the option by using this script: 在当前情况下,通过使用以下脚本单击选项,将div #content加载到#get_loaded_content

$(document).ready(function(){ 
$("#choose_content_to_load").change(function(){ 
    var selectedOption = $('#choose_content_to_load :selected').val(); 
    $containerDiv = $('#get_loaded_content'); 
    $containerDiv.html("");
       switch (selectedOption)
       {
        case "Some content from page content.html and div #content":$containerDiv.load( "content.html #content" , function(){$(document).trigger("reload_javascript");});break;
       }
    return true;
    }); 
});

As you see the script also trigger a "reload" of all the scripts "reload_javascript". 如您所见,脚本还触发所有脚本“ reload_javascript”的“重新加载”。 This is becuse the div #content have some more design elements that needs to be executed at the load in. These scripts looks like this: 这是因为div #content还有一些设计元素需要在加载时执行。这些脚本如下所示:

$(document).on("ready reload_javascript" ,function() {
script
});

This works fine and all the loaded elements initializes and works, this becuse Index.html and content.html share the same design scripts (same .js file) so the scripts just need to "run again" to work. 这可以正常工作,并且所有加载的元素都可以初始化和工作,这是因为Index.htmlcontent.html共享相同的设计脚本(相同的.js文件),因此脚本只需“再次运行”即可工作。 Now to the problem, the div #content need to have a larger script that only execute when the div is being loaded into #get_loaded_content in Index.html . 现在解决问题,div #content需要有一个更大的脚本,该脚本仅在将div加载到Index.html #get_loaded_content中时才执行。 Its not good to have the script in the .js file that both index.html and content.html share becuse its alot of code. index.htmlcontent.html共享的.js文件包含在脚本中是不好的,因为它包含许多代码。

So the new script need to be put direct into the #content html using tags and execute when this div is being loaded in. 因此,需要使用标签将新脚本直接放入#content html中,并在加载此div时执行。

First I thought the new script will run just by adding the reload_javascript but then I realised that dident execute the script just initialize it (I think). 首先,我认为新脚本仅通过添加reload_javascript就可以运行,但后来我意识到执行脚本只是初始化它(我认为)。 I hope somebody can help me with this and please have in mind that I try to learn jQuery (beginner at coding). 我希望有人可以帮助我解决这个问题,请记住我尝试学习jQuery(编码入门)。

Thanks alot. 非常感谢。

If i understand you correctly you are loading <script> tags into content and wish for it to execute? 如果我正确理解您的意思,您正在将<script>标记加载到内容中并希望其执行? If so call this function i made a while back after insertion. 如果是这样,请在插入后回拨一段时间。

/**
*This function is supposed to execute script after script insertions
*/
function execute(){
    $('#content script').each(function() {
        var _script = document.createElement('script');
        _script.type = 'text/javascript';
        _script.setAttribute('async', 'true');
    /**
    *IEFIX (IE does not support .innerHTML on createElement)
    **/
        if(!_script.innerHTML){
        _script.text=$(this).html().replace('\\\\',"\\");
        }else{
        _script.innerHTML  = $(this).html().replace('\\\\',"\\");
        }
        document.body.appendChild(_script); 
    });
}

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

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