[英].load() page doesn't run jquery
I'm using .load() functions to add content to my page, but I can't get any jquery scripts to work on the loaded content. 我正在使用.load()函数向页面添加内容,但是我无法让任何jquery脚本来处理加载的内容。
Here's an example of the loaded page and script: 这是加载的页面和脚本的示例:
<div class="content">
<div class="sections">
<div id="holder">
<div class="resumelink" id="educ">
Education
</div>
<div class="resumelink" id="work">
Work
</div>
<div class="resumelink" id="skills">
Skills
</div>
</div>
</div>
<div class="resumecontent">
blah blah blah
</div>
<script src="jquery-2.0.3.min.js" type="text/javascript" ></script>
<script>
$('#educ').click(function () {
// any script here
});
$('#work').click(function () {
// any script here
});
$('#skills').click(function () {
// any script here
});
</script>
</div>
There's some other posts that seems to have the same problem, but none of them seem to be helping me. 还有其他一些帖子似乎也有同样的问题,但似乎没有一个对我有帮助。 No scripts seem to be working no matter what I try - not when on the initial page that loaded the content - can't even get a console.log script to work.
无论我如何尝试,脚本似乎都无法正常工作-在加载内容的初始页面上无法运行-甚至无法使console.log脚本正常工作。
The SOLUTION , provided by @Archer - is simply to add a jquery() tag and add to the original page instead of the loaded page.
@Archer提供的SOLUTION-只是添加jquery()标记并添加到原始页面而不是已加载的页面。 An example:
一个例子:
jQuery(function($) {
$(document).on("click", "#educ", function() {
// any function here
});
});
Thanks! 谢谢!
Remove all the script references from the html that you load - they will not be executed when using .load()
... 从您加载的html中删除所有脚本引用-使用
.load()
时将不会执行它们。
<div class="content">
<div class="sections">
<div id="holder">
<div class="resumelink" id="educ">
Education
</div>
<div class="resumelink" id="work">
Work
</div>
<div class="resumelink" id="skills">
Skills
</div>
</div>
</div>
<div class="resumecontent">
blah blah blah
</div>
</div>
Then, in your original page (or preferably an included script file), add the following... 然后,在您的原始页面(最好是包含的脚本文件)中,添加以下内容...
jQuery(function($) {
$(".content").on("click", "#educ", function() {
// any script here
});
$(".content").on("click", "#work", function() {
// any script here
});
$(".content").on("click", "#skills", function() {
// any script here
});
});
I think you want to do it like this: 我认为您想这样做:
$('#container').load("content.html", function () {
$('#educ').click(function () {
// any script here
});
$('#work').click(function () {
// any script here
});
$('#skills').click(function () {
// any script here
});
});
I don't think Javascript gets executed inside a load call, so you need to put it in the callback to get it to execute. 我认为Javascript无法在加载调用中执行,因此您需要将其放入回调中才能执行。
Bind event after DOM ready, use this code DOM准备就绪后绑定事件,请使用此代码
<script>
$(document).ready(function(){
$('#educ').click(function () {
// any script here
});
$('#work').click(function () {
// any script here
});
$('#skills').click(function () {
// any script here
});
});
</script>
$(“ .resumecontent”).load(“ insert.html”);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.