在严格的javascript中在事件处理程序中使用此代码? [英] Using this in event handler in strict javascript?

查看:67
本文介绍了在严格的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.

在这里发现: https://stackoverflow.com/a/16553290/552067

这篇关于在严格的javascript中在事件处理程序中使用此代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆