在函数内部引用 this
给定代码(其中大部分被删除),
// A data set
$.DataArea = function() {
// Default options
$.extend(this, {
'class': "DataSet",
'bars': new Array(),
'container': null,
'containerId': null,
'gridsize': 20,
'width': 400,
'height': 400,
'currentSelectedBar': 0
});
// Add a bar to this object
this.addBar = function(startDate, endDate, label, styleID) {
// When the bar is clicked
$('#' + barID).click(function() {
alert($(this).currentSelectedBar);
if (this.currentSelectedBar != undefined)
$('#' + this.currentSelectedBar).fadeIn("slow");
this.currentSelectedBar = barID;
$('#' + barID).fadeTo("slow", 0.5);
});
}
当我 alert($(this).currentSelectedBar);
时,它总是显示为未定义,它没有正确设置值。有什么想法可能是为什么吗?
这个想法是,当您单击一个栏时,它会淡出,当您单击另一个栏时,最后一个淡出的栏也会淡入。
Given the code (a lot of it stripped out)
// A data set
$.DataArea = function() {
// Default options
$.extend(this, {
'class': "DataSet",
'bars': new Array(),
'container': null,
'containerId': null,
'gridsize': 20,
'width': 400,
'height': 400,
'currentSelectedBar': 0
});
// Add a bar to this object
this.addBar = function(startDate, endDate, label, styleID) {
// When the bar is clicked
$('#' + barID).click(function() {
alert($(this).currentSelectedBar);
if (this.currentSelectedBar != undefined)
$('#' + this.currentSelectedBar).fadeIn("slow");
this.currentSelectedBar = barID;
$('#' + barID).fadeTo("slow", 0.5);
});
}
When I alert($(this).currentSelectedBar);
it always comes out as undefined, it's not setting the value properly. Any ideas why this might be?
The idea is when you click on a bar, it fades it out, and when you click another the last bar to fade out fades in as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您在
DataArea
实例上调用addBar
,您的代码应该是:在
click
处理程序中,this
将引用 DOM 元素'#' + barID
。但这些属性被分配给其他某个元素,而不是该 DOM 元素。Assuming you are calling
addBar
on an instance ofDataArea
, your code should be:Inside the
click
handler,this
will refer to the DOM element'#' + barID
. But the properties are assigned to some other element and not that DOM element.回调函数中的
this
指的是引发事件的元素:在这种情况下为$('#' + barID)
。我猜您想访问您扩展的
this
。在这种情况下,你应该写这样的东西:The
this
in your callback function refers to the element that rises the event : in that case$('#' + barID)
.I guess you want to access the
this
that you extended. In that case, you should write something like that :将
this
的值缓存在点击函数之外。在 click 函数中,上下文this
是被单击的 DOM 节点。Cache the value of
this
outside the click function. inside the click function, the contextthis
is the DOM node that was clicked.您似乎对 jQuery 的行为有疑问。
$('#' + barID).click(function() {
重新映射this
的定义,但之前尝试向其添加属性将不会产生任何效果,因为this
的定义已被覆盖,正如 recmashak 所提到的,您可以将原始
this
放入变量中,然后在执行警报时使用它。It looks like you're having an issue with the behavior of jQuery.
$('#' + barID).click(function() {
remaps the definition ofthis
, but having attempted to add attributes to it previously will have no effect because the definition ofthis
has been overwritten.As mentioned by recmashak you can put the original
this
into a variable and then use it when you do your alert