Javascript 模拟点击事件(点击链接与html点击) 兼容IE/Firefox


一把情况下模拟点击一般两个方面,模拟点击超级连接事件
firefox的兼容的函数为
对HTMLAnchorElement 加入onclick事件

复制代码 代码如下:

try {
// create a element so that HTMLAnchorElement is accessible
document.createElement('a');
HTMLElement.prototype.click = function () {
if (typeof this.onclick == 'function') {
if (this.onclick({type: 'click'}) && this.href)
window.open(this.href, this.target? this.target : '_self');
}
else if (this.href)
window.open(this.href, this.target? this.target : '_self');
};
}
catch (e) {
// alert('click method for HTMLAnchorElement couldn\'t be added');
}

下面是具体的应用