How to dynamically include a html file containing javascript

后端 未结 2 609
Happy的楠姐
Happy的楠姐 2021-01-25 19:39

in my website i have a simple navigation bar where clicking on the different items causes the main content to change. Technically, all is done in a single file \"main.html\", wh

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-25 20:38

    You can trigger a custom event to notify your other.html script that the tree is loaded. I haven't tried it, but I would use something like that :

    In main.html:

    $('#div_to_load').load('other.html',{},function(){
        $('#div_to_load').trigger('loaded')
    })
    

    In other.html

    $('#div_to_load').on('loaded', function(){
         // Code to execute  
    })
    

    I don't know if that could do the trick.

    Edit : I asume that the javascript directly included in your other.html is executed anyway.

提交回复
热议问题