在HTML从AJAX的处理结果没有jQuery的事件 [英] No jQuery events handled on HTML from AJAX results

查看:107
本文介绍了在HTML从AJAX的处理结果没有jQuery的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个网页,使用填充一个AJAX请求div的内容。

I've created a web page that populates the content of a div using an AJAX request.

我用

$('#div_id').html(data); 

填充div和这工作得很好。

to populate the div and this works fine.

但是,所有返回的内容已经失去了JQuery的,没有更多的onclick事件并没有jQueryUI的部件。

However all of the returned content has lost its JQuery, no more onclick events and no JQueryUI widgets.

我已经打过电话了所有这些

I've tried calling all of these

$('#div_id').trigger('create');
$('#div_id').html(data).trigger('create');
$('body').trigger('create');

和所有同pagecreate,而不是创造。

and all the same with 'pagecreate' rather than create.

这一切都不是工作,尽管一切,我已经找到了,而谷歌搜索是有关jQuery Mobile的,所以我可以完全找错了树。

None of this is working although everything I've found whilst googling is related to JQuery Mobile so I could be completely barking up the wrong tree.

感谢

推荐答案

您需要使用事件委派,当你添加新的元素,这样的DOM。

You need to use event delegation when you append new elements to the DOM like that.

很难给出一个确切的答案没有看到的HTML,但说你有,你有在单击处理程序添加一个按钮...你会装上处理器的#div_id,而不是像这样的:

Hard to give an exact answer without seeing the HTML, but say you have a button that you're adding with an on click handler... You would attach the handler to the #div_id instead like this:

$('#div_id').on('click', 'button', function () {
  //do your thing...
});

在对方法的第二个参数是将分派事件的元素。你可以认为它像一个如果的声明。该处理程序安装于祖先元素,当事件冒泡到它时,事件被触发。当它的触发,即第二个参数被选中。它说:做的按钮调度此事件?,如果答案是是的回调函数运行。

The second argument in the on method is the element that will dispatch the event. You can think of it like an "if" statement. The handler is attached to the ancestor element and when the event bubbles up to it, the event is triggered. When it's triggered, that second argument is checked. It says "did 'button' dispatch this event?", if the answer is "yes" the function callback runs.

这篇关于在HTML从AJAX的处理结果没有jQuery的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆