简体   繁体   English

让我的jQuery在.load()页面上工作

[英]Getting my jQuery to work on .load() pages

I'm trying to get my javascript to work on pages I load through the .load command. 我正在尝试让我的JavaScript在通过.load命令加载的页面上运行。

$("#loadingDockShotPg").load("include/shot_comments.php");

I'm doing this to load a subsection of a page, but the problem is, the div within the newly loaded page isn't being affected by my jQuery file that I have in the parent file. 我正在执行此操作以加载页面的子部分,但是问题是,新加载的页面中的div不受父文件中包含的jQuery文件的影响。 What is a better way to load content so that the newly injected can be affected by my javascript? 有什么更好的方式来加载内容,以便新注入的内容可以受到我的JavaScript的影响?

I'm assuming that you're issue is with event listeners. 我假设您遇到的问题是事件监听器。

If, for example you're using: 例如,如果您使用的是:

$('.something').click(function(){ 
   // DO SOMETHING 
});

Try changing it out for: 尝试将其更改为:

$(document).on('click','.something',function(){ 
   // DO SOMETHING 
});

This way the jQuery will listen for any clicks on the document and apply the action to events that match clicking on the .something object. 这样,jQuery将侦听对文档的任何单击,并将操作应用于与单击.something对象匹配的事件。

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

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