我目前使用此代碼綁定在谷歌瀏覽器擴展事件:Javascript - 突出顯示懸停元素的事件?
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);
});
,並將其與click事件效果很好。 現在,我的問題是:我可以使用什麼事件來改變某個元素被徘徊而不是簡單地點擊?最終目標是改變被光標懸停的元素的背景顏色。
我試過mouseover,mouseenter,focus和focusin無濟於事。確切地說,當事件觸發時,我試圖做一個console.log(),但它從來沒有真正發生過,除了有一次我點擊一個對話框並且它檢測到我對這個元素的焦點。
我目前正在使用chrome(v24.0),但跨瀏覽器的解決方案將是一個不錯的功能,因爲我打算以後在Firefox上重複使用腳本。儘管這不是重中之重。
爲什麼你'elem.attachEvent'檢查?如果你正在構建Google Chrome瀏覽器擴展程序,則無需爲IE8補貨... –
@BenjaminGruenbaum嗯......你永遠不會知道。如果Chrome擴展系統移植到舊的IE版本會怎樣?你會做什麼? –