替换元素后点击事件不起作用

发布于 2024-12-11 19:49:43 字数 1462 浏览 0 评论 0原文

我得到了这个代码:

$(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独﹏钓一江月 2024-12-18 19:49:43

使用 live 方法,即使在 DOM 更新后也会绑定事件 http://api.jquery.com/live/

$(function () {
    $('#or').live('click', function () {
        $("#m0").html('<input type="text" class="highlight" name="test1"/>');
    });
});

Use the live method, which binds events even after DOM updates http://api.jquery.com/live/

$(function () {
    $('#or').live('click', function () {
        $("#m0").html('<input type="text" class="highlight" name="test1"/>');
    });
});
踏月而来 2024-12-18 19:49:43

更新此答案已过时。查看 JQuery.on,它结合了两全其美的优点。它允许您将事件绑定到元素,而事件本身则针对该元素内的元素。它就像 live 一样,只有您可以选择特定的目标元素(例如添加/更改 Ajaxed 内容的容器元素)。 on 取代了 livebinddelegate

原始答案(适用于1.7之前的JQuery):正如答案所建议的,JQuery的live方法是最简单的方法,但它有一些缺点。它比较慢,因为对 DOM 的每次修改都会导致所有“活动”选择器被重新评估。

另请参阅此答案以了解一些缺点: Performance Difference Between jQuery's .live(' click', fn) 和 .click(fn)

因此,我宁愿使用 bindclick 将 click 事件显式绑定到新的创建的元素。这需要更多的工作(尽管如果您正确构建代码,则可以忽略不计),但它可以为您提供更好的性能和更多的控制。

Update This answer is was 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 like live, only you can choose a specific target element, (for instance a container element in which Ajaxed content is added/changed). on replaces live, bind and delegate.

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 or click 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.

十六岁半 2024-12-18 19:49:43

您必须使事件处理程序live

$('#or').live("click", function() {
    //Event hander code
});

来自 的 jQuery 文档直播

将处理程序附加到与当前匹配的所有元素的事件
选择器,现在和将来。

问题是事件处理程序绑定到绑定时 DOM 中存在的元素。当该元素被删除时,事件处理程序也随之被删除。当您添加一个新元素(它是一个新元素,即使它看起来与旧元素相同)时,它没有绑定到它的事件处理程序。这就是 live 派上用场的地方。它监视 DOM 中添加与选择器匹配的元素,并将事件处理程序应用于任何添加的元素。

You'll have to make the event handlers live:

$('#or').live("click", function() {
    //Event hander code
});

From the jQuery docs for live:

Attach a handler to the event for all elements which match the current
selector, now and in the future.

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.

∝单色的世界 2024-12-18 19:49:43

.click(...) 替换为 .live("click", ...)

Replace .click(...) with .live("click", ...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文