jQuery .live() 与 .on() 方法在加载动态 html 后添加点击事件

发布于 2024-12-25 07:43:36 字数 485 浏览 0 评论 0原文

我正在使用 jQuery v.1.7.1,其中 .live() 方法显然已被弃用。

我遇到的问题是,当使用以下方法将 html 动态加载到元素中时:

$('#parent').load("http://..."); 

如果我尝试添加单击事件,它不会使用以下方法之一注册该事件:

$('#parent').click(function() ...); 

或者

// according to documentation this should be used instead of .live()
$('#child').on('click', function() ...); 

实现此功能的正确方法是什么?它似乎只适用于 .live() 对我来说,但我不应该使用该方法。请注意,#child 是动态加载的元素。

谢谢。

I am using jQuery v.1.7.1 where the .live() method is apparently deprecated.

The problem I am having is that when dynamically loading html into an element using:

$('#parent').load("http://..."); 

If I try and add a click event afterwards it does not register the event using either of these methods:

$('#parent').click(function() ...); 

or

// according to documentation this should be used instead of .live()
$('#child').on('click', function() ...); 

What is the correct way to achieve this functionality? It only seems to work with .live() for me, but I shouldn't be using that method. Note that #child is a dynamically loaded element.

Thanks.

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

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

发布评论

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

评论(7

走走停停 2025-01-01 07:43:36

如果您希望单击处理程序适用于动态加载的元素,则可以在父对象(不会动态加载)上设置事件处理程序,并为其提供一个与动态对象匹配的选择器,如下所示

$('#parent').on("click", "#child", function() {});

:将附加到 #parent 对象,并且每当源自 #child 的点击事件冒泡到该对象时,它都会触发您的点击处理程序。这称为委托事件处理(事件处理被委托给父对象)。

这样做是因为即使 #child 对象尚不存在,但当它稍后存在并被单击时,您也可以将事件附加到 #parent 对象,单击事件将冒泡到 #parent 对象,它将看到它源自 #child 并且有一个用于单击 #child 的事件处理程序 并触发您的事件。

If you want the click handler to work for an element that gets loaded dynamically, then you set the event handler on a parent object (that does not get loaded dynamically) and give it a selector that matches your dynamic object like this:

$('#parent').on("click", "#child", function() {});

The event handler will be attached to the #parent object and anytime a click event bubbles up to it that originated on #child, it will fire your click handler. This is called delegated event handling (the event handling is delegated to a parent object).

It's done this way because you can attach the event to the #parent object even when the #child object does not exist yet, but when it later exists and gets clicked on, the click event will bubble up to the #parent object, it will see that it originated on #child and there is an event handler for a click on #child and fire your event.

回心转意 2025-01-01 07:43:36

试试这个:

$('#parent').on('click', '#child', function() {
    // Code
});

$.on() 文档:

事件处理程序仅绑定到当前选定的元素;他们
当您的代码调用 .on() 时,页面上必须存在。

当您调用 $.on() 时,您的 #child 元素不存在,因此事件未绑定(与 $.live() 不同) )。然而,#parent 确实存在,因此将事件绑定到它就可以了。

上面代码中的第二个参数充当“过滤器”,仅在事件从 #child 冒泡到 #parent 时触发。

Try this:

$('#parent').on('click', '#child', function() {
    // Code
});

From the $.on() documentation:

Event handlers are bound only to the currently selected elements; they
must exist on the page at the time your code makes the call to .on().

Your #child element doesn't exist when you call $.on() on it, so the event isn't bound (unlike $.live()). #parent, however, does exist, so binding the event to that is fine.

The second argument in my code above acts as a 'filter' to only trigger if the event bubbled up to #parent from #child.

◇流星雨 2025-01-01 07:43:36

$(document).on('click', '.selector', function() { /* do stuff */ });

编辑:我提供了有关其工作原理的更多信息,因为……的话。
在本示例中,您将在整个文档上放置一个侦听器。

当您单击任何与.selector匹配的元素时,事件就会冒泡到主文档 - 只要没有其他侦听器调用事件.stopPropagation() 方法——该方法会将事件冒泡到父元素。

您不是绑定到特定元素或元素集,而是侦听来自与指定选择器匹配的元素的任何事件。这意味着您可以一次性创建一个侦听器,该侦听器将自动匹配当前现有元素以及任何动态添加的元素。

这是明智的,有几个原因,包括性能和内存利用率(在大型应用程序中)

编辑:

显然,您可以监听的最接近的父元素更好,并且您可以使用任何元素来代替 document 只要您想要监视事件的子元素位于该父元素内...但这实际上与问题没有任何关系。

$(document).on('click', '.selector', function() { /* do stuff */ });

EDIT: I'm providing a bit more information on how this works, because... words.
With this example, you are placing a listener on the entire document.

When you click on any element(s) matching .selector, the event bubbles up to the main document -- so long as there's no other listeners that call event.stopPropagation() method -- which would top the bubbling of an event to parent elements.

Instead of binding to a specific element or set of elements, you are listening for any events coming from elements that match the specified selector. This means you can create one listener, one time, that will automatically match currently existing elements as well as any dynamically added elements.

This is smart for a few reasons, including performance and memory utilization (in large scale applications)

EDIT:

Obviously, the closest parent element you can listen on is better, and you can use any element in place of document as long as the children you want to monitor events for are within that parent element... but that really does not have anything to do with the question.

陌上青苔 2025-01-01 07:43:36

1.7 中的 .live() 等效项如下所示:

$(document).on('click', '#child', function() ...); 

基本上,观察文档中的单击事件并过滤它们以查找 #child。

The equivalent of .live() in 1.7 looks like this:

$(document).on('click', '#child', function() ...); 

Basically, watch the document for click events and filter them for #child.

瑾夏年华 2025-01-01 07:43:36

我知道现在回答有点晚了,但我已经为 .live() 方法创建了一个polyfill。我已经在 jQuery 1.11 中测试过它,它似乎工作得很好。我知道我们应该尽可能实现 .on() 方法,但在大型项目中,无论出于何种原因都不可能将所有 .live() 调用转换为等效的 .on() 调用,以下可能work:

if(jQuery && !jQuery.fn.live) {
    jQuery.fn.live = function(evt, func) {
        $('body').on(evt, this.selector, func);
    }
}

只需在加载 jQuery 之后和调用 live() 之前包含它即可。

I know it's a little late for an answer, but I've created a polyfill for the .live() method. I've tested it in jQuery 1.11, and it seems to work pretty well. I know that we're supposed to implement the .on() method wherever possible, but in big projects, where it's not possible to convert all .live() calls to the equivalent .on() calls for whatever reason, the following might work:

if(jQuery && !jQuery.fn.live) {
    jQuery.fn.live = function(evt, func) {
        $('body').on(evt, this.selector, func);
    }
}

Just include it after you load jQuery and before you call live().

十级心震 2025-01-01 07:43:36

.on() 适用于 jQuery 1.7 及以上版本。如果您有旧版本,请使用以下命令:

$("#SomeId").live("click",function(){
    //do stuff;
});

.on() is for jQuery version 1.7 and above. If you have an older version, use this:

$("#SomeId").live("click",function(){
    //do stuff;
});
不可一世的女人 2025-01-01 07:43:36

我在项目中使用了“live”,但我的一位朋友建议我应该使用“on”而不是 live。
当我尝试使用它时,我遇到了像您一样的问题。

在我的页面上,我动态创建按钮表行和许多 dom 内容。但是当我使用的时候魔法就消失了。

其他解决方案就像像孩子一样使用它,每次点击时都会调用您的函数。
但我找到了一种方法让它再次发生,这就是解决方案。

将代码编写为:

function caller(){
    $('.ObjectYouWntToCall').on("click", function() {...magic...});
}

Call caller();在页面中创建对象后,如下所示。

$('<dom class="ObjectYouWntToCall">bla... bla...<dom>').appendTo("#whereeveryouwant");
caller();

通过这种方式,当您的函数不应该每次点击页面时都会被调用。

I used 'live' in my project but one of my friend suggested that i should use 'on' instead of live.
And when i tried to use that i experienced a problem like you had.

On my pages i create buttons table rows and many dom stuff dynamically. but when i use on the magic disappeared.

The other solutions like use it like a child just calls your functions every time on every click.
But i find a way to make it happen again and here is the solution.

Write your code as:

function caller(){
    $('.ObjectYouWntToCall').on("click", function() {...magic...});
}

Call caller(); after you create your object in the page like this.

$('<dom class="ObjectYouWntToCall">bla... bla...<dom>').appendTo("#whereeveryouwant");
caller();

By this way your function is called when it is supposed to not every click on the page.

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