委托/ on with hover

时间:2012-04-10 01:07:59

标签: jquery delegates

  

可能重复:
  Using delegate() with hover()?

我正在尝试这段代码,但显然我做错了。使用此代码,hover效果不起作用。

但是:$('.group>span').hover (function() {之类的内容很有效但我需要委托html()内容。

<div class="group">
    <span></span>
</div>

$('.group>span').delegate("hover", "a", function() {
    $(this).html('<a href="#new_list">Some button</a>');
}, function() {
    $(this).empty();
});​

有什么想法吗?感谢

2 个答案:

答案 0 :(得分:0)

hover只是mouseentermouseleave附近的便利包装器。我想你必须分开处理每一个。

答案 1 :(得分:0)

传递给delegate的参数数量无效。你传递 4 ,而它只需要3。

.delegate( selector, eventType, handler(eventObject) )是函数签名 代码中的参数顺序错误!你交换了选择器和事件类型

你写了这个:

$('.group>span').delegate ("hover", "a", function() {

虽然它应按此顺序排列:

$('.group>span').delegate ("a", "hover", function() {

无论如何,您可以delegate使用hover这样:

$('.group>span').delegate("a", "mouseenter", function() {
    $(this).html('<a href="#new_list">Some button</a>');
});

$('.group>span').delegate("a", "mouseleave", function() {
    $(this).empty();
}​);

LIVE DEMO
你可以在这个问题中看到另一种方式:
Using delegate() with hover()?