替换元素后点击事件不起作用
我得到了这个代码:
$(function () {
$('#or').click(function () {
$("#m0").html('<input type="text" class="highlight" name="test1"/>');
});
});
它工作正常,它确实取代了:
使用文本输入,当我点击 #or
点击其他地方(然后它被替换为原始内容)时,问题出现,当我添加原始内容时,它不起作用。
这是我用来将替换的 #m0
上的 text input
替换为原始代码的代码:
$(function () {
$('.m').click(function () {
$("#m0").html('<label><input type="radio" id="or" name="type" value="1"/>Click me</label>');
});
});
所以,为了确保您确实理解我:
原始内容是:
< code>
然后我用这个来替换上面的使用文本输入:
$(function () {
$('#or').click(function () {
$("#m0").html('<input type="text" class="highlight" name="test1"/>');
});
});
然后...我使用此代码恢复原始内容:
$(function () {
$('.m').click(function () {
$("#m0").html('<label><input type="radio" id="or" name="type" value="1"/>Click me</label>');
});
});
最后当:
替换为:
点击事件不再起作用。
I got this code:
$(function () {
$('#or').click(function () {
$("#m0").html('<input type="text" class="highlight" name="test1"/>');
});
});
Its working fine and it does replace the:
<label><input type="radio" id="or" name="type" value="1"/>Click me</label>
with the text input, the problem appear when I click on #or
click somewhere else (then it is replacing with original content), and when I add the original content back, it doesn't work.
This is the code I used to replace the text input
on replaced #m0
with the original one:
$(function () {
$('.m').click(function () {
$("#m0").html('<label><input type="radio" id="or" name="type" value="1"/>Click me</label>');
});
});
So, to be sure you did understand me:
The original content is:
<label><input type="radio" id="or" name="type" value="1"/>Click me</label>
then I use this to replace the above with text input:
$(function () {
$('#or').click(function () {
$("#m0").html('<input type="text" class="highlight" name="test1"/>');
});
});
and then... I use this code to restore back the original content:
$(function () {
$('.m').click(function () {
$("#m0").html('<label><input type="radio" id="or" name="type" value="1"/>Click me</label>');
});
});
And finally when:
<input type="text" class="highlight" name="test1"/>
is replaced with:
<label><input type="radio" id="or" name="type" value="1"/>Click me</label>
The click event doesnt work anymore.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 live 方法,即使在 DOM 更新后也会绑定事件 http://api.jquery.com/live/
Use the live method, which binds events even after DOM updates http://api.jquery.com/live/
更新此答案
是已过时。查看 JQuery.on,它结合了两全其美的优点。它允许您将事件绑定到元素,而事件本身则针对该元素内的元素。它就像live
一样,只有您可以选择特定的目标元素(例如添加/更改 Ajaxed 内容的容器元素)。on
取代了live
、bind
和delegate
。原始答案(适用于1.7之前的JQuery):正如答案所建议的,JQuery的
live
方法是最简单的方法,但它有一些缺点。它比较慢,因为对 DOM 的每次修改都会导致所有“活动”选择器被重新评估。另请参阅此答案以了解一些缺点: Performance Difference Between jQuery's .live(' click', fn) 和 .click(fn)
因此,我宁愿使用
bind
或click
将 click 事件显式绑定到新的创建的元素。这需要更多的工作(尽管如果您正确构建代码,则可以忽略不计),但它可以为您提供更好的性能和更多的控制。Update This answer
iswas outdated. Check JQuery.on, which combines the best of both worlds. It allows you to bind an event to an element, while the event itself is targeted to elements inside that element. It's likelive
, only you can choose a specific target element, (for instance a container element in which Ajaxed content is added/changed).on
replaceslive
,bind
anddelegate
.Original answer (for JQuery before 1.7): JQuery's
live
method, as suggested by the answers, is the easy way out, but it has some disadvantages. It is slower, because each modification to the DOM causes all 'live' selector to be re-evaluated.See also this answer for some disadvantages: Performance difference between jQuery's .live('click', fn) and .click(fn)
Therefore, I rather use
bind
orclick
to explicitly bind the click event to the newly created elements. It is a little more work (though neglectable if you structure your code properly) and it gives you better performance and more control.您必须使事件处理程序
live
:来自
的 jQuery 文档直播
:问题是事件处理程序绑定到绑定时 DOM 中存在的元素。当该元素被删除时,事件处理程序也随之被删除。当您添加一个新元素(它是一个新元素,即使它看起来与旧元素相同)时,它没有绑定到它的事件处理程序。这就是
live
派上用场的地方。它监视 DOM 中添加与选择器匹配的元素,并将事件处理程序应用于任何添加的元素。You'll have to make the event handlers
live
:From the jQuery docs for
live
:The problem is that the event handler is bound to the element that exists in the DOM at the time of binding. When that element is removed, the event handler is removed with it. When you add a new element (it's a new element, even if it does look the same as the old one) it doesn't have the event handler bound to it. That's where
live
comes in handy. It monitors the DOM for the addition of elements that match the selector, and applies the event handler to any that do.将
.click(...)
替换为.live("click", ...)
Replace
.click(...)
with.live("click", ...)