[英]Confusion Over jQuery Events?
I have just started to use jQuery and have run into a problem regarding events.我刚刚开始使用 jQuery 并且遇到了有关事件的问题。
Below is my code, it creates a div element on the page, I would like to attach an event handler to this div that reacts if it is clicked or double clicked.下面是我的代码,它在页面上创建了一个 div 元素,我想将一个事件处理程序附加到这个 div 上,如果它被单击或双击它就会做出反应。 I thought the following code would work but it doesn't seem to work at all:我认为下面的代码会起作用,但它似乎根本不起作用:
this.mainAppDiv = document.createElement("div");
this.mainAppDiv.id = "mainBody";
this.mainAppDiv.style.width = "99%";
this.mainAppDiv.style.height = this.mainCanvasDiv_H;
this.mainAppDiv.style.border = "thin red dashed";
document.body.appendChild(this.mainAppDiv);
$(this.mainAppDiv).click(function()
{
alert("The Clicked Divs ID: " + this.mainAppDiv.id);
});
When the event is fired is should simply alert what DIV element was clicked but I get the following error in firebug:触发事件时应该简单地提醒单击了哪个 DIV 元素,但我在 firebug 中收到以下错误:
this.mainAppDiv is undefined this.mainAppDiv 未定义
line 43 - alert("The Clicked Divs ID: " + this.mainAppDiv.id);第 43 行 - alert("点击的 Divs ID:" + this.mainAppDiv.id);
Can anyone see why this would not work?谁能明白为什么这行不通?
You're guilty of this
abuse.你犯了this
虐待罪。
-- this == window -- 这 == 窗口
$(this.mainAppDiv).click(function()
{ $(this.mainAppDiv).click(function()
{
-- this == dom object clicked -- this == dom 对象被点击
alert("The Clicked Divs ID: " + this.mainAppDiv.id);
Simple answer is change to:简单的答案是更改为:
$(this.mainAppDiv).click(function(e)
{
alert("The Clicked Divs ID: " + e.target.id);
});
But bigger picture use selectors instead of this
to find things.但更大的图景是使用选择器而不是this
来查找东西。 this
is not portable and should be avoided unless you really want to refer to a specific context. this
不是可移植的,除非您真的想引用特定的上下文,否则应该避免这样做。 And it can be redefined - if you have, say, a function in an object which uses this
to refer to the object it's part of, and you bind that to a jQuery click event, this
would refer to thing that was clicked, and not the object.并且它可以被重新定义——如果你在一个对象中有一个函数,它使用this
来引用它所属的对象,并且你将它绑定到一个 jQuery click 事件, this
将引用被点击的东西,而不是物体。 Your code wouldn't work at all if you moved it to a function or object.如果您将代码移至函数或对象,您的代码将根本无法工作。 eg do this instead.例如,改为这样做。
$('#mainAppDiv').click(..)
this
is not the same element within the handler. this
与处理程序中的元素不同。 Inside the handler, this
refers to the element that was clicked to which the handler was applied.在处理程序内部, this
指的是应用了处理程序的被单击的元素。
$('div').attr('id','mainBody')
.css( { width: '99%',
height: $('#mainCanvasDiv').height(),
border-style: 'dashed',
border-width: 'thin',
border-color: 'red'
})
.click( function() {
alert('The clicked Divs ID: ' + this.id );
})
.appendTo('body');
Because this
inside that event handler will not refer to the same object as it did when the handler was defined and passed to jQuery.因为事件处理程序内部的this
不会像定义处理程序并将其传递给 jQuery 时那样引用相同的对象。 It'll be a reference to the target <div>
you created (assuming that setup for the event handler actually works).它将是对您创建的目标<div>
的引用(假设事件处理程序的设置确实有效)。
You can simplify that code a lot:您可以大大简化该代码:
var self = this;
${'body').append($('<div></div>', {
id: 'mainBody',
css: {width: '99%', height: self.mainCanvasDiv_H, borderColor: 'red', borderWidth: 'thin', borderStyle: 'dashed' },
click: function() {
alert("Clicked ID: " + this.id);
}
}));
As they are created runtime , you have to use live instead of click由于它们是运行时创建的,因此您必须使用 live 而不是 click
http://api.jquery.com/live/ http://api.jquery.com/live/
jquery live should help jquery live应该有帮助
As others mentioned you have two problems in your code , you still need to use jquery live...as you are creating the div dynamically正如其他人提到的那样,您的代码中有两个问题,您仍然需要实时使用 jquery ...因为您正在动态创建 div
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.