有了 jQuery,我如何使用“实时”功能?添加新元素时发出警报的方法?

发布于 2024-11-18 04:31:07 字数 473 浏览 7 评论 0原文

首先,我不想让另一个插件来执行此操作...jQuery 已经具有使用 live() 方法完成我想要的 99% 功能的功能。

基本上,我想将其更改

$(".some-button").live("click", function() { alert('yay!'); });

为:

$(".some-button").live(function() { alert('yay!'); });

所以,我的意思是,当将元素添加到页面时,我想立即触发一个方法... jQuery 已经可以做到这一点 SOOO close< /strong> 因为这就是它添加“点击”的方式,即使在上面的示例中也是如此!

如何在不添加另一个插件的情况下实现这一目标,而只需使用与“live”和“die”方法相同的功能?

First off, I don't want another plugin to do this... jQuery already has the functionality to do 99% of what I want with the live() method.

Basically, I want to change this:

$(".some-button").live("click", function() { alert('yay!'); });

into this:

$(".some-button").live(function() { alert('yay!'); });

So, what I'm saying is that I want to fire a method right away when an element is added to the page... jQuery can already do this SOOO close because that's how it adds the "click" even in my example above!

How can I achieve this without adding another plugin, but rather, just by using the same functionality as the "live" and "die" methods do?

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

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

发布评论

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

评论(3

冰魂雪魄 2024-11-25 04:31:07

这是我复制和粘贴的一些代码,它们似乎可以在小提琴中工作,至少在 FF 上: http://jsfiddle.net /KQBzn/

$(document).bind('DOMNodeInserted', function(event) {
      alert('inserted ' + event.target.nodeName + // new node
            ' in ' + event.relatedNode.nodeName); // parent
});

来源:http://forum.jquery.com/topic/how-to-get-notified-when-new-dom-elements-are-addition

Here's some code I've copied and pasted that seems to work in fiddle, at least on FF: http://jsfiddle.net/KQBzn/

$(document).bind('DOMNodeInserted', function(event) {
      alert('inserted ' + event.target.nodeName + // new node
            ' in ' + event.relatedNode.nodeName); // parent
});

source: http://forum.jquery.com/topic/how-to-get-notified-when-new-dom-elements-are-added

感悟人生的甜 2024-11-25 04:31:07

没有任何跨浏览器的方法可以做到这一点,并且 jQuery 本身也没有允许这样做。

您必须使用插件,或者只是手动管理新元素的调用代码。

live()[docs]< /sup> 方法和 delegate() [文档]方法是仅用于事件处理。您提供给他们的代码只会响应浏览器事件,而不响应 DOM 操作。


.live() 不会执行此操作的原因是它在向 DOM 添加新元素时不会运行任何代码。它不监视任何 DOM 更改。相反,它响应冒泡到文档的事件,并在与您提供的选择器匹配时调用处理程序。

There isn't any cross-browser way to do this, and there's nothing in jQuery itself that allows it.

You'd have to use a plugin, or just manage invoking code for your new elements manually.

The the live()[docs] method and the delegate()[docs] method are only for event handling. The code you give them will only respond to browser events, not to DOM manipulation.


The reason .live() won't do this is because it does not run any code when adding new elements to the DOM. It isn't monitoring any DOM changes. Rather it is responding to events that bubble to the document, and invoking the handler if it matches the selector you gave it.

探春 2024-11-25 04:31:07

你不能用 .live() 方法来做到这一点。

看来 jQuery 应该向 .live() 方法添加一个功能,这样如果它与特定关键字(如“created”而不是事件名称)一起使用,那么它将允许您为创建的元素执行一个函数。那就太酷了!例如,理想的场景是这样的:

$('.foobar').live('created', function() {
    // do something with each newly created .foobar element here.
});

You can't do it with the .live() method.

It seems jQuery should add a feature to the .live() method so that if its used with a specific keyword like 'created' instead of an event name then it will let you execute a function for the created element. That'd be cool! For example, the ideal scenario would be like this:

$('.foobar').live('created', function() {
    // do something with each newly created .foobar element here.
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文