Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
交叉浏览器Javascript侦听器_Javascript_Listener - Fatal编程技术网

交叉浏览器Javascript侦听器

交叉浏览器Javascript侦听器,javascript,listener,Javascript,Listener,我的下一个代码工作正常: 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",

我的下一个代码工作正常:

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);
       });
}

但这并不是事件的导火索。似乎是
如果(links[index].addEventListener)
未传递。知道为什么吗

嗯,上面的代码是正确的。问题是我没有清除缓存。这是我以前拥有但没有工作的东西:

if( window.addEventListener ) { 
    ...
但是


正在正常工作。

作为最后的回退,您可以通过onclick属性添加事件。您应该使用逻辑来定义sperate函数,以检查附加事件处理程序的可用方法

function addCrossBrowserEvent(element, eventName, callback){
    if(element.addEventListener){
        element.addEventListener(eventName, callback);
    }
    else if(element.attachEvent){
        element.attachEvent('on' + eventName, callback);
    }
    else{
        element['on' + eventName] = callback;
    }
}
用法:

addCrossBrowserEvent(myElement, 'click', function() { 
    alert('clicked'); 
});
注意:您也可以尝试将其设计为HtmleElement原型的扩展

HTMLElement.prototype.addCrossBrowserEvent = function(eventName, callback) {
    if(this.addEventListener){
        this.addEventListener(eventName, callback);
    }
    else if(this.attachEvent){
        this.attachEvent('on' + eventName, callback);
    }
    else{
        this['on' + eventName] = callback;
    }
}

// Usage
document.getElementById('elementId').addCrossBrowserEvent('click', function() { 
    // ... 
});

你有任何控制台错误吗?@TanzeelKazi-没有错误。。。
addCrossBrowserEvent(myElement, 'click', function() { 
    alert('clicked'); 
});
HTMLElement.prototype.addCrossBrowserEvent = function(eventName, callback) {
    if(this.addEventListener){
        this.addEventListener(eventName, callback);
    }
    else if(this.attachEvent){
        this.attachEvent('on' + eventName, callback);
    }
    else{
        this['on' + eventName] = callback;
    }
}

// Usage
document.getElementById('elementId').addCrossBrowserEvent('click', function() { 
    // ... 
});