简体   繁体   中英

Load script tag from the file html

In the header of the first page

<script type="text/javascript">
$(document).ready(function(){
  $("#register_reg").click(function(){
    $("#register_form").load("test_test.php #register_rules");
  });
});
</script>

In the content of the ID #register_rules in the second file (test_test.php) there are this line

<script type="text/javascript" src="jquerycode.js"></script>

The probleme is, in the fist page, when i click the link (ID #register_reg) the line script not added in the ID #register_form

So, how I can add the content of the ID #register_rules with here script in the ID #register_form

The script is stripped out of the HTML loaded.

From the jQuery docs about load() (emphasis added)

http://api.jquery.com/load/

Script Execution

When calling .load() using a URL without a suffixed selector expression, the content is passed to .html() prior to scripts being removed. This executes the script blocks before they are discarded. If .load() is called with a selector expression appended to the URL, however, the scripts are stripped out prior to the DOM being updated , and thus are not executed. An example of both cases can be seen below:

Here, any JavaScript loaded into #a as a part of the document will successfully execute.

1 $( "#a" ).load( "article.html" ); However, in the following case, script blocks in the document being loaded into #b are stripped out and not executed:

1 $( "#b" ).load( "article.html #target" );

this is the correct code

<script type="text/javascript">
$(document).ready(function(){
  $("#register_reg").click(function(){
    $("#register_form").load("test_test.php #register_rules", function() { 
        $("#register_form").append("<script type='text/javascript' src='jquerycode.js'></" + "script>"); 
    });
  });
});
</script>

Try this.

$("#register_form").load("test_test.php #register_rules",function(){
    $.getScript("jquerycode.js"); 
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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