var $inputPswd1 = $('#checkInput01'),
$inputPswd2 = $('#checkInput02'),
$inputPswds = $inputPswd1.add($inputPswd2);
var bindOn = function () {
return typeof $(document)['on'] ? 'on' : 'bind';
}();
$inputPswds[bindOn]('keyup._passchk', function(e){
});
此代码片段是作为输入验证的一部分编写的。谁能告诉我这些行的作用:
typeof $(document)['on']
和
$inputPswds[bindOn]('keyup._passchk', function(e) {});
checkInput01
和 checkInput02
是两个输入的 id。
最佳答案
typeof $(document)['on']
这用于检查 DOM 对象上的 on
方法是否可用。在 jQuery 1.7 版本之前,它将不可用。
$inputPswds[bindOn]('keyup._passchk', function(e) {});
这会根据上面语句的结果调用 on
或 bind
方法,并将一个 keyup
事件处理程序分配给$inputPswds
jQuery 对象中的元素。
请注意,如果您使用 1.7 以后的任何版本的 jQuery,则该检查几乎是多余的,可以删除,只需调用 on()
关于javascript - ']' 上的 'typeof $(document)[' 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40605646/