简体   繁体   English

HTML是但onclick函数后javascript无法在div中呈现

[英]html is but javascript is not rendering in div after onclick function

I'm using a button onclick function to load content into a div called #content on my index page. 我正在使用按钮onclick函数将内容加载到索引页上名为#content的div中。 Following is the on click javascript function. 以下是点击Javascript函数。

function myFunction() {

   $('#content').load("content/page1.php");

};

It works fine, but when it grabs the loaded php file, all the javascript specifically on that page1.php is not rendered in the #content div. 它工作正常,但当它抓取已加载的php文件时,该page1.php上的所有专门的javascript都不会在#content div中呈现。 The html renders fine. 的HTML呈现良好。 And if you type the absolute path of page1.php in the browser address bar, the page1.php javascript also renders fine. 而且,如果您在浏览器地址栏中键入page1.php的绝对路径,则page1.php javascript也可以正常显示。

Any ideas on how to make the javascript render in that #content div? 关于如何在该#content div中呈现javascript的任何想法? I'm using a newer version of chrome. 我使用的是chrome的较新版本。

According to the documentation for load , 根据有关负载的文档,

When calling .load() using a URL without a suffixed selector expression, the content is passed to .html() prior to scripts being removed. 使用不带后缀选择器表达式的URL调用.load()时,将在删除脚本之前将内容传递给.html()。 This executes the script blocks before they are discarded . 这将在脚本块被丢弃之前执行它们。

I would suggest you consider restructuring your javascript so that its loaded in a module within the parent page if it makes sense, as it makes things much easier to debug. 我建议您考虑重组javascript,以便在有意义的情况下将其加载到父页面中的模块中,因为这样可以使调试更加容易。 If that isn't an option, the script blocks are still executed... 如果这不是一个选项,则脚本块仍将执行...

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

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