Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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_Oop_Object_Mouseevent - Fatal编程技术网

将鼠标事件绑定到javascript对象

将鼠标事件绑定到javascript对象,javascript,oop,object,mouseevent,Javascript,Oop,Object,Mouseevent,有没有办法将鼠标事件(如“mouseover”)绑定到JavaScript对象 function test(x,y,z){ this.x = x; this.y = y; this.z = z; this.add = function(){ $('body').append("<div id='"+this.z+"'>test: x:"+this.x" y:"+this.y"</div>"); } return this.

有没有办法将鼠标事件(如“mouseover”)绑定到JavaScript对象

function test(x,y,z){
   this.x = x;
   this.y = y;
   this.z = z;
   this.add = function(){
       $('body').append("<div id='"+this.z+"'>test: x:"+this.x" y:"+this.y"</div>");
   }
   return this.add();
}

t1 = new test(1,2,3);
t2 = new test(2,3,4);
功能测试(x,y,z){
这个.x=x;
这个。y=y;
这个。z=z;
this.add=函数(){
$('body').append(“test:x:+this.x”y:+this.y”);
}
返回这个。add();
}
t1=新试验(1,2,3);
t2=新试验(2,3,4);

在我的代码中,我在以下结构中定义了一个类/对象,是否有任何方法将事件侦听器附加到附加的div,但可以访问对象属性。

您可以在将其添加到正文后执行此操作,因此在
this.add
函数中可以绑定事件:

$("#"+this.z).mouseover(function(){
     // Function
});
但你知道身份证不能以数字开头,对吗

如果要使用
this.z
,请执行此操作。我使用的是
div[0]
,因为所有jQuery对象都是数组,所以第0个元素就是我刚才创建的元素

function test(x,y,z){
   var div = $("<div id='"+this.z+"'>test: x:"+this.x+" y:"+this.y+"</div>").appendTo("body");
   div[0].y = y;
   div[0].x = x;
   div[0].z = z;
   div.mouseover(function(){
       alert(this.x);
   });

   return div;
}

t1 = new test(1,2,3);
t2 = new test(2,3,4);
功能测试(x,y,z){
var div=$(“测试:x:+this.x+“y:+this.y+”).appendTo(“正文”);
div[0].y=y;
div[0].x=x;
div[0].z=z;
div.mouseover(函数(){
警报(this.x);
});
返回div;
}
t1=新试验(1,2,3);
t2=新试验(2,3,4);

您可以在将其添加到正文后执行此操作,因此在
this.add
函数中,您可以绑定事件:

$("#"+this.z).mouseover(function(){
     // Function
});
但你知道身份证不能以数字开头,对吗

如果要使用
this.z
,请执行此操作。我使用的是
div[0]
,因为所有jQuery对象都是数组,所以第0个元素就是我刚才创建的元素

function test(x,y,z){
   var div = $("<div id='"+this.z+"'>test: x:"+this.x+" y:"+this.y+"</div>").appendTo("body");
   div[0].y = y;
   div[0].x = x;
   div[0].z = z;
   div.mouseover(function(){
       alert(this.x);
   });

   return div;
}

t1 = new test(1,2,3);
t2 = new test(2,3,4);
功能测试(x,y,z){
var div=$(“测试:x:+this.x+“y:+this.y+”).appendTo(“正文”);
div[0].y=y;
div[0].x=x;
div[0].z=z;
div.mouseover(函数(){
警报(this.x);
});
返回div;
}
t1=新试验(1,2,3);
t2=新试验(2,3,4);

请避免使用JavaScript将元素添加到页面的DOM中 HTML

HTML代表内容,就像JavaScript代表行为一样。重要规则 为了一个好的代码

只需创建DIV元素(它将是一个普通对象)并执行任何操作 您想添加它,从添加事件侦听器到添加事件侦听器

var div = document.createElement('div');

div.setAttribute('id', 'foobar');
div.textContent = 'lol';

div.addEventListener('mouseover', myEventCallback, false);

document.body.appendChild(div);
通过使用一个像样的JavaScript库,您可以将上面的代码减少到两行
干净的代码。

请避免使用JavaScript将元素添加到页面的DOM中 HTML

HTML代表内容,就像JavaScript代表行为一样。重要规则 为了一个好的代码

只需创建DIV元素(它将是一个普通对象)并执行任何操作 您想添加它,从添加事件侦听器到添加事件侦听器

var div = document.createElement('div');

div.setAttribute('id', 'foobar');
div.textContent = 'lol';

div.addEventListener('mouseover', myEventCallback, false);

document.body.appendChild(div);
通过使用一个像样的JavaScript库,您可以将上面的代码减少到两行
干净的代码。

为回复喝彩,但是我尝试了这一点,并从这个函数中进行了尝试,即
$(“#”+this.z).mouseover(函数(){alert(this.x)})
我无法访问对象的属性,例如this.x/this.y,关于ID是我的错,我只是输入了随机文本。这是因为您创建的
div
是新元素,更新了我的帖子。为回复干杯,但是我已经尝试了这一点,并且在这个函数中也尝试了这一点,即
$(“#”+this.z)。鼠标悬停(函数(){alert(this.x) });我无法访问对象的属性,例如this.x/this.y,关于ID的问题,我只是输入了随机文本。这是因为您创建的
div
是新元素,更新了我的帖子。