.live() 与 .on() 方法

发布于 2025-01-04 06:01:49 字数 278 浏览 4 评论 0原文

我正在开发一个项目,在该项目中,当我持续按下分/加按钮而不使用 .live() 方法将鼠标悬停在图片上时,该函数就会起作用。对于 .on() 方法,该函数不起作用。

如何解决此问题,使其也适用于 .on() 方法?

这是我所指内容的示例(我修复了此示例中的错误,但我使用 .on 方法是错误的)。

I'm working on a project, in which when I keep pressing on the min/plus button without hovering off the picture with the .live() method, the function works. In the case of .on() method the function does not work.

How can I fix this issue, so it works for .on() method as well?

Here is an example of what I’m referring too (I fixed the error in this example, but I was using the .on method wrong).

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

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

发布评论

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

评论(2

浮生面具三千个 2025-01-11 06:01:49

你没有正确使用它。 .live() 的替换是 $(document).on() ,当然,事件和处理程序被传入......例如:

$(document).on('click', '#myElement', function() { 
  //... some function ...
});

值得一提在 .on() 出现之前,.live() 已经被认为是处理此类绑定的低效方法。建议改用 .delegate(),现在改为 .on() (使用委托者语法)。

或者举个例子:您应该选择不会因 DOM 操作而被破坏的最近的祖先,而不是将文档作为侦听器(这是 .live() 过去所做的)。老实说,我发现“jsdo.it”使用起来有点笨拙,所以我没有想到特定的元素,但例如,给定结构:

<div id="ajax_container">
 <button id="do_something">Clicky!</button>
 <p>Some dynamically-loaded content</p>
</div>

其中 ajax_container 的内容被替换为Ajax 调用(无需显示该部分的代码),为该按钮的单击事件绑定一个未销毁的侦听器(容器 div),如下所示:

$('#ajax_container').on('click', '#do_something', function() {
 // do something
})

You're not using it correctly. The replacement for .live() is $(document).on() with the event and handler being passed in, of course... for example:

$(document).on('click', '#myElement', function() { 
  //... some function ...
});

It's worth mentioning that before .on() ever came around, .live() was already considered an inefficient way to handle this kind of binding. .delegate() was recommended instead, and now .on() (using the delegator syntax).

Or as an example: instead of the document being the listener (which is what .live() used to do), you should pick the nearest ancestor that does not get destroyed with DOM manipulations. I honestly find the "jsdo.it" a bit clunky to use so I don't have the specific element in mind, but for example, given the structure:

<div id="ajax_container">
 <button id="do_something">Clicky!</button>
 <p>Some dynamically-loaded content</p>
</div>

Where the contents of ajax_container are replaced by an Ajax call (no need to show the code for that part), binding a non-destroyed listener (the container div) for that button's click event would look like:

$('#ajax_container').on('click', '#do_something', function() {
 // do something
})
°如果伤别离去 2025-01-11 06:01:49

您必须发布一些代码才能确定为什么您的实现不起作用,但是您应该查看 jQuery 源代码以获取如何使用 .on() 和 .live() 的示例。

<div id="parent">
 <a href="#" id="anchor">Click Me</a>
</div>

$('#anchor').live('click',function() { }):
$('#parent').on('click','#anchor',function() { });
$('#anchor').on('click',function() { });

.live() 事件侦听器是添加到文档元素,以便只要事件一直冒泡到文档元素,就会触发回调

在第二个示例( $('#parent').on() )中,事件侦听器添加到父元素,每次点击事件时都会触发冒泡到 #parent 并来自(或沿途与其交互)名为 #anchor 的元素

。第三个示例 ( $('#anchor').on() )将事件侦听器直接添加到锚元素本身,并且是与 $('#anchor').bind('click',function() { }) 完全相同;

引入 .live() 的原因是,如果页面结构发生变化,事件回调仍然可以被触发,因为事件侦听器本身附加到文档元素。

您可以在类似的方法中使用 on,但它必须将自身附加到未从页面中删除的元素 - 如果该元素被删除,那么所有事件侦听器也会随之删除。

http://api.jquery.com/on/
http://api.jquery.com/live/

You'll have to post some code to be certain as to why your implementation was not working, but you should view the jQuery source for examples of how to use .on() and .live()

<div id="parent">
 <a href="#" id="anchor">Click Me</a>
</div>

$('#anchor').live('click',function() { }):
$('#parent').on('click','#anchor',function() { });
$('#anchor').on('click',function() { });

The .live() event listener is added to the document element so that the callback will be fired as long as the event bubbles all the way up to the document element

In the second example ( $('#parent').on() ), the event listener is added to the parent element and is fired every time a click event bubbles up to #parent and comes from (or interacts with along the way) an element named #anchor

The third example ( $('#anchor').on() ) adds the event listener directly to the anchor element itself and is the exact same as $('#anchor').bind('click',function() { });

The reason why .live() was introduced was so that if your page structure changes, event callbacks can still be fired because the event listener itself is attached to the document element.

You can use on in a similar method, but it has to be attached itself to an element that is not removed from the page - if that element is removed, then so are all the event listeners along with it.

http://api.jquery.com/on/
http://api.jquery.com/live/

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