使用处理程序删除元素时的最佳实践是什么?

发布于 2025-01-06 05:57:10 字数 372 浏览 1 评论 0原文

当删除我的委托处理程序所附加的元素时,我在 IE8 上使用 jQuery 的 .delegate() 方法时遇到了问题。场景如下:我有一个表,其中的行每行都有一个删除按钮。行是动态创建的,因此删除按钮是使用委托来连接的,以处理单击事件。当需要删除最后一行时,整个表都被删除(只有在有一个值来创建至少一行时才会创建表。)除了 IE8 之外,几乎所有地方都可以正常工作。即便如此,它在某些页面上工作,但不是一个页面(每个页面上都使用完全相同的代码。)对于这个特定页面,删除最后一行将在 IE8 中生成一个 js 错误,指出“需要对象” - 违规的委托的 jQuery 代码中的行。

所以问题是,当删除处理程序正在使用的一些 DOM 元素时,处理程序的最佳实践是什么?是否应该在某个时刻调用取消委托,如果是,在哪里调用?

I encountered an issue using jQuery's .delegate() method on IE8 when removing the elements to which my delegate handler was attached. Here's the scenario: I had a table with rows that each had a delete button. The rows were created dynamically, so the delete button was wired up using delegate to handle the click events. When it came time to remove the last row, the entire table was removed (table was only created once there was a value to create at least one row.) This worked fine just about everywhere with the one exception of IE8. Even then, it worked on some pages, but just not one (the exact same code was used on each page.) For this one particular page, deleting that last row would generate a js error in IE8 stating "Object required" - the offending line being in the jQuery code for delegate.

So the question is, what's the best practice with handlers when some of the DOM elements those handlers are using are removed? Should undelegate be called at some point, and if so, where?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

于我来说 2025-01-13 05:57:10

使用 jQuery 的 .remove() 方法

“当您想要删除元素本身及其内部的所有内容时,请使用.remove()。除了元素本身之外,与元素关联的所有绑定事件和 jQuery 数据也会被删除。已删除。”

或者使用 .empty() 方法 删除所有子项但保留顶部水平元素。

Use jQuery's .remove() method:

"Use .remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed."

Or the .empty() method to remove all children but keep the top level element.

千紇 2025-01-13 05:57:10

Kinakuta,根据您的说法,这可能只是正确设置委托并适当地表达删除的问题,而不是使用或不使用 remove() 本身。很难理解为什么只有 IE8 会出现错误,但 jQuery 中可能存在一些 IE8 问题。

在 jQuery 1.7+ 中, .on() 取代了 .bind().live().delegate() > 并可能提供前进的道路。我希望做这样的事情:

$("#myContainer").on("click", "button.delete", function(event){
    $(this).closest("tr", "#myContainer").remove();
});

如果您使用 jQuery <1.7 ,您无疑可以使用 .delegate() 实现类似的效果。

据我所知,在行删除时,表和/或其行是使用 MVC 或任何其他生成方法生成的,这并不重要。

Kinakuta, from what you say it may simply be question of setting up the delegation correctly and phrasing the removal appropriately, rather than the use or not of remove() per se. It's hard to see why only IE8 should misbehave but probably some IE8 issue in jQuery.

With jQuery 1.7+ .on() replaces .bind(), .live() and .delegate() and may offer a way ahead. I would be looking to do something like this:

$("#myContainer").on("click", "button.delete", function(event){
    $(this).closest("tr", "#myContainer").remove();
});

You can no doubt achieve similar with .delegate() if you are using jQuery <1.7 .

As far as I can tell, it shouldn't matter, at the point of row removal, that the table and/or its rows were generated with MVC or any other generation method.

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