在严格的javascript中在事件处理程序中使用此代码?
问题描述:
假设您有一个类似于以下的例程来连接Click事件处理程序
Suppose you have a routine like the following to wire up click event handlers
getElements(".board>div").forEach(function(elem){
elem.addEventListener("click", handleClick);
});
然后在处理程序中,您需要与发件人(即此人)一起工作
And then in the handler, you need to work with the sender (i.e. this)
function handleClick(){
if(this.innerText.toLowerCase() !== "x"){
...
在这种情况下,如何在没有jshint违反/警告的情况下使用this
?
How do you use this
in this scenario without a jshint violation/warning?
答
您对this
的使用有效.要消除事件处理程序中的this
错误,请在函数顶部添加/*jshint validthis: true */
.
Your use of this
is valid. To suppress the this
errors in your event handler add /*jshint validthis: true */
to the top of the function.