简体   繁体   English

两个要素-一键单击,另一悬停,一个功能

[英]two elements - one click, another hover, one function

Im trying to make button1 and button2 have each trigger, but same function. 我试图使button1和button2具有每个触发器,但功能相同。

Something like this: 像这样:

$("#button1").hover,$("#button2").click (function () {...

The function parameter can take in a pointer to a function. 函数参数可以接受指向函数的指针。

so: 所以:

$("#button1").hover(DoSomething);
$("#button2").click(DoSomething);

function DoSomething() {
     //does something...
}

use .bind() which allows you to bind multiple events to an object. 使用.bind()允许您将多个事件绑定到一个对象。

$(obj).bind('hover, click',function(e){.....})

see here: http://jsfiddle.net/uc9W9/ 看到这里: http : //jsfiddle.net/uc9W9/

EDIT::: 编辑:::

use .on() instead if you are using newer versions of jquery as .bind() may not be used in future versions of jquery 如果您使用的是jquery的较新版本,请改用.on() ,因为将来的jquery版本中可能不会使用.bind()

see here: http://jsfiddle.net/uc9W9/1/ 看到这里: http : //jsfiddle.net/uc9W9/1/

You could better define a event handler function once and bind the same in both the events. 您最好一次定义一个事件处理函数,然后在两个事件中都绑定相同的函数。

var callback = function() { ... }
$('#button1').hover(callback);
$('#button2').click(callback);

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

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