【发布时间】:2022-01-24 21:32:48
【问题描述】:
我目前正在使用此代码在 Google Chrome 扩展程序中绑定事件:
var bindEvent = function(elem ,evt,cb) {
//see if the addEventListener function exists on the element
if ( elem.addEventListener ) {
elem.addEventListener(evt,cb,false);
//if addEventListener is not present, see if this is an IE browser
} else if ( elem.attachEvent ) {
//prefix the event type with "on"
elem.attachEvent('on' + evt, function(){
/* use call to simulate addEventListener
* This will make sure the callback gets the element for "this"
* and will ensure the function's first argument is the event object
*/
cb.call(event.srcElement,event);
});
}
};
/* ... */
bindEvent(document,'click', function(event)
{ var target = event.target || event.srcElement;
/*Code to do stuff about a clicked element*/
this.removeEventListener('click',arguments.callee,false);
});
它适用于点击事件。 现在,我的问题是:我可以使用什么事件来更改悬停而不是简单地单击的元素?最终目标是更改被光标悬停的元素的背景颜色。
我试过mouseover、mouseenter、focus 和focusin 无济于事。确切地说,我尝试在事件触发时执行console.log(),但它从未真正发生过,除了有一次我单击一个对话框并检测到我对这个元素的关注。
我目前正在使用 Chrome (v24.0),但跨浏览器解决方案将是一个不错的功能,因为我计划稍后在 Firefox 上重用该脚本。不过这不是首要任务。
【问题讨论】:
-
你为什么要检查
elem.attachEvent?如果您正在构建 Google Chrome 扩展程序,则无需为 IE8 填充... -
@BenjaminGruenbaum 嗯......你永远不会知道。如果 Chrome 扩展系统移植到旧的 IE 版本怎么办?那么你会怎么做呢?
标签: javascript google-chrome-extension dom-events mouseevent