Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获得;这";在没有显式调用的事件处理程序内部;这";_Javascript_Html_Css_Class - Fatal编程技术网

Javascript 如何获得;这";在没有显式调用的事件处理程序内部;这";

Javascript 如何获得;这";在没有显式调用的事件处理程序内部;这";,javascript,html,css,class,Javascript,Html,Css,Class,我明白了: class-Myclass{ 构造函数(){ this.img=document.querySelector(“img”); this.greeBox=document.querySelector(“.aux”); this.changeColor=this.changeColor.bind(this);//我在这里绑定侦听器 this.items=document.queryselectoral(“.container[data color]”); 此.actualColor=“

我明白了:

class-Myclass{
构造函数(){
this.img=document.querySelector(“img”);
this.greeBox=document.querySelector(“.aux”);
this.changeColor=this.changeColor.bind(this);//我在这里绑定侦听器
this.items=document.queryselectoral(“.container[data color]”);
此.actualColor=“”;
this.auxAddListeneres();
this.addListeneres();
}
auxAddListeneres(){
this.greeBox.addEventListener(“mousedown”,this.auxChangeColor);
this.greeBox.addEventListener(“touchstart”,this.auxChangeColor);
}
auxChangeColor(e){
e、 preventDefault();例如stopPropagation();
this.style.backgroundColor=this.dataset.color;
//this.actualColor=this.dataset.color-->无法执行此操作,因为“this”属于处理程序
//所以为了解决这个问题,我应该将它绑定到构造函数
}
RemoveAuxAddListeners(){
this.greeBox.removeEventListener(“mousedown”,this.auxChangeColor);
this.greeBox.removeEventListener(“touchstart”,this.auxChangeColor);
}
变色(e){
e、 preventDefault();例如stopPropagation();
//不是“this”是构造函数
this.actualColor=“???????”;
console.log(this);
console.log(如target);
//问题是我不能在处理程序中使用与“this”类似的等价物
//不,e.target并不总是对象处理程序,因为e.target可以是图像
//里面
}
addListeneres(){

对于(让i=0;i使用而不是
e.target
,它总是指处理程序所附加的元素。

使用而不是
e.target
,它总是指处理程序所附加的元素。

谢谢!我不知道该属性是解决方案,因为在发布问题之前,我在e控制台,它没有显示任何类似的内容,再次查看,我在控制台“currentTarget”中看到它显示为null,但在使用它时,它实际上是工作的谢谢!我不知道该属性是解决方案,因为在发布问题之前,我在控制台中查看事件,它没有显示任何类似的内容,再次查看时,我发现在控制台“currentTarget”中它显示为null,但在使用它时,它实际上是工作的