![](/img/trans.png)
[英]Binding event handler to the click event using jQuery on() wouldn't work on future elements
[英]Why wouldn't 'this' keyword work on an event handler function that uses parameters?
var x = document.getElementById('id');
x.addEventListener('click', function() {
check(x, 5);
}, false);
function check(x, length) {
var msg = document.getElementById('ids');
if (this.value.length < length) { //doesn't work
msg.innerHTML = 'not long enough'
}
}
if(x.value.length< length)
有效。 我不完全理解如何实现this
关键字。
this
inside check 将指向window
。 你必须通过将this
绑定到它来call
函数check
来完成你的任务,
x.addEventListener('click',function(){
check.call(this, x,5);
}, false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.