简体   繁体   English

在通过jQuery的load()函数加载的HTML上使用已经加载的JS?

[英]Use JS that is already loaded, on HTML loaded via jQuery's load() function?

Not sure if this is posible or not, but I'm trying to use JavaScript that I loaded via a script tag when the page was initially requested on dynamic HTML that was loaded via jQuery's load() function. 不确定这是否可行,但是当我最初在通过jQuery的load()函数加载的动态HTML上请求页面时,我正在尝试使用通过script标记加载的JavaScript。

Example: http://jsfiddle.net/3MR43/ 示例: http//jsfiddle.net/3MR43/

As you can see in the example, after clicking a link in the pop up box, the link goes. 正如您在示例中所看到的,在弹出框中单击链接后,链接就会出现。 The problem is that the link was suppose to be stopped, and you were suppose to see an alert. 问题是链接被假定为已停止,您可能会看到警报。

However, if I paste the HTML that I am loading via jQuery, it works, so the code is fine. 但是,如果我粘贴我通过jQuery加载的HTML,它可以工作,所以代码很好。

Description 描述

You need jQuery .live() or .on() method to bind events to dynamically created html. 您需要jQuery .live().on()方法将事件绑定到动态创建的html。

Choose .live() or .on() depending on the version of jQuery you are using. 根据您使用的jQuery版本选择.live().on()

.live() Available since jQuery 1.3. .live()自jQuery 1.3起可用。 Attach an event handler for all elements which match the current selector, now and in the future. 为现在和将来与当前选择器匹配的所有元素附加事件处理程序。

.on() Available since jQuery 1.7. .on()自jQuery 1.7起可用。 Attach an event handler function for one or more events to the selected elements. 将一个或多个事件的事件处理函数附加到所选元素。

Check out my sample and this jsFiddle Demonstration 看看我的样本和这个jsFiddle演示

Sample 样品

... for jQuery.live() ...对于jQuery.live()

$('.lang').live("click", function(e) {   
    e.preventDefault();  
    alert('Testing..');        
});

... for jQuery.on() ...对于jQuery.on()

$('.lang').on("click", function(e) {   
    e.preventDefault();  
    alert('Testing..');        
});

More Information 更多信息

The problem is your .click will do it for only elements that exist at that time . 问题是你的.click只会为当时存在的元素执行此操作 If you want to do it for all potential future elements that that selector will match, you want to use on() ( delgate() or live() with older versions of jQuery). 如果要对该选择器将匹配的所有潜在的未来元素执行此操作,则需要使用on()delgate()live()与旧版本的jQuery)。

The $('.lang').click() event must be registered after the element is created! 必须在创建元素后注册$('.lang').click()事件!

You need a callback on the load function to register this. 您需要在load函数上进行回调以注册它。

You need to use live to attach handlers to elements that are loaded dynamically. 您需要使用live来将处理程序附加到动态加载的元素。

Try this, it will solve your problem. 试试这个,它会解决你的问题。

$('.lang').live('click',function(e) { $('。lang')。live('click',function(e){

    e.preventDefault();

    alert('Testing..');

});

You have to use on function for dynamicly loaded elements: 您必须对动态加载的元素使用on函数:

    $('.open_lang').click(function(e) {      
    e.preventDefault();    
    $('#popup').load('http://skins.thehabbos.org/pop_up/language.php').css('display', 'block');            
});

$('body').on('click', '.lang', function(e) {    
    e.preventDefault();  
    alert('Testing..');        
});

( live and delegate are deprecated in JQuery 1.7+ which is the version of your JSFiddle) livedelegate在JQuery 1.7+中被弃用,这是你的JSFiddle的版本)

Fixed JSFiddle 修复了JSFiddle

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

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