1 つのフォーム ID を queryselectorall メソッドに渡すと、フォーム内のすべての入力を表示できます。しかし、フォームの 2 つの ID を queryselector all に渡すと、2 つのフォームが返されます。誰かがこれで私を助けることができますか? 以下は、私が使用しているコードです
window.$ = function(el) {
el = document.querySelectorAll(el);
el.size = el.length;
if (el.length === 1) {
el[0].size = 1;
return el[0];
} else {
return el;
}
};
完全なコード:
window.onload=function(){
(function (window) {
window.Q = function (el) {
el = document.querySelectorAll(el);
el.size = el.length;
if (el.length === 1) {
el[0].size = 1;
return el[0];
} else {
return el;
}
};
window.addEvents = function (el, ev, fn) {
var i,
el = (typeof el == 'string') ? Q(el) : el,
ev = (ev.indexOf(' ')) ? ev.split(' ') : [ev];
ev.forEach(function (ea) {
if (el.length) {
for (var i = 0; i < el.length; i++) {
el[i].addEventListener(ea, fn, false);
}
} else {
console.log(el, ea, fn)
el.addEventListener(ea, fn, false);
}
});
};
window.callme = function (e) {
console.log(e.type)
}
window.init = function () {
/*Below code works when i bind events to one form and all the input fields gets the events attached */
//window.addEvents('#form1', 'focus keyup blur', callme);
/* Uncomment the below line to reproduce the issue only the key up event gets fired and that is also getting attached to form and not to form elements*/
window.addEvents('#form1,#form2', 'focus keyup blur', callme);
}
})(window);
window.init();