当我将一个表单 ID 传递给 queryselectorall 方法时,我能够看到表单内的所有输入。但是当我将表单的两个 id 传递给 queryselector 时,我看到它返回了两个表单。有人可以帮我解决这个问题吗?下面是我使用的代码
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();