我有下面的代碼正常工作:跨瀏覽器的Javascript監聽
var links = document.getElementsByClassName('register');
for(var index = 0; index < links.length; ++index)
{
links[index].addEventListener('click', function(){
var newMixpanelEvent = 'mixpanel.track("Click On Searched List", {"user is logged": "no"})';
trackEvent(newMixpanelEvent);
});
}
這只是監聽點擊事件,然後執行一個函數來創建一個事件Mixpanel。
現在,我需要檢查addEventListener
功能和attachEvent
,使其工作,幾乎所有的瀏覽器,所以我做的:
var links = document.getElementsByClassName('register');
for(var index = 0; index < links.length; ++index)
{
if(links[index].addEventListener) {
links[index].addEventListener('click', function(){
var newMixpanelEvent = 'mixpanel.track("Click On Searched List", {"user is logged": "no"})';
trackEvent(newMixpanelEvent);
});
} else if(links[index].attachEvent) {
links[index].attachEvent('onclick', function(){
var newMixpanelEvent = 'mixpanel.track("Click On Searched List", {"user is logged": "no"})';
trackEvent(newMixpanelEvent);
});
}
}
但這不是射擊的事件。好像if(links[index].addEventListener)
沒有通過。任何想法爲什麼?
您是否收到任何控制檯錯誤? –
@TanzeelKazi - 沒有錯誤... – Manolo