简体   繁体   English

如何将脚本与dom元素解除绑定?

[英]How to unbind scripts from dom element?

I have script that triggers on hover and on click events on specific tag 我有在悬停和特定标签上的点击事件触发的脚本

$(".dropdown-button").dropdown({ 
  hover: true, // Activate on hover
});

Unfortunately, triggering onclick event is set by default. 不幸的是,默认情况下会设置触发onclick事件。

How Can i forbid triggering script on click event? 如何禁止点击事件触发脚本? I need it to be triggered only on hover. 我需要仅在悬停时触发它。

I need it only in this particular case for this specific tag .dropdown-button . 我只在这种特殊情况下才需要此特定标记.dropdown-button

update 更新

I use materializecss http://materializecss.com/dropdown.html 我使用materializecss http://materializecss.com/dropdown.html

.off() disables onhover event, but not click. onhover .off()禁用onhover事件,但不单击。

To forbid triggering script on click event it would be good if you overwrite the click event. 要禁止在click事件上触发脚本,最好覆盖一下click事件。 You can bind this click event with a empty function. 您可以将此点击事件与一个空函数绑定在一起。

You probably can do this 你可能可以做到这一点

var hoverTimer;
var hoverDelay = 200;

$('.dropdown-button').hover(function() {
    var $target = $(this);
    // on mouse in, start a timeout
    hoverTimer = setTimeout(function() {        
        // do your stuff here
        $target.trigger('click');         
    }, hoverDelay);
}, function() {
    // on mouse out, cancel the timer
    clearTimeout(hoverTimer);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM