[英]Javascript scope of object property
Let's say I want to make a Javascript class for boxes in my page. 假设我想为页面中的框创建一个Javascript类。 When an object of that class is made, I want to add a click event to it that would require me accessing one or many of its unique properties.
创建该类的对象时,我想向其添加click事件,这将需要我访问其一个或多个唯一属性。 How would I do that?
我该怎么做? Here's an example:
这是一个例子:
function Box(boxClassName, originalColor, changeColor){
this.boxClassName = boxClassName;
this.originalColor = originalColor;
this.changeColor = changeColor;
this.initialize = function (){
var html = '<div class="' + this.boxClassName + '></div>';
$(document).append(html);
$(this.boxClassName).css("background-color", originalColor);
// Toggle box color on click
$(this.boxClassName).click(function (){
// this.boxClassName is undefined here, none of this works
if ($("." + this.boxClassName).css("background-color") == originalColor) {
$("." + this.boxClassName).css("background-color", this.changeColor);
} else {
$("." + this.boxClassName).css("background-color", this.originalColor);
}
});
};
this.initialize();
}
You cannot to access property of Box in onclick event. 您无法在onclick事件中访问Box的属性。 If you would like to store data of html element, you have to use http://api.jquery.com/jquery.data/ .
如果要存储html元素的数据,则必须使用http://api.jquery.com/jquery.data/ 。 By the way, below code might work for you purpose.
顺便说一句,下面的代码可能适合您的目的。
if ($(this).css("background-color") == originalColor) {
$(this).css("background-color", this.changeColor);
} else {
$(this).css("background-color", this.originalColor);
}
There are two ways: 有两种方法:
1) Keep a reference to this in a variable and use that: 1)在变量中保留对此的引用并使用:
// Toggle box color on click
var self = this;
$(this.boxClassName).click(function () {
if ($("." + self.boxClassName).css("background-color") == originalColor) {
$("." + self.boxClassName).css("background-color", self.changeColor);
} else {
$("." + self.boxClassName).css("background-color", self.originalColor);
}
});
2) Or bind the click callback to the Box 2)或将click回调绑定到Box
$(this.boxClassName).click(function () {
if ($("." + this.boxClassName).css("background-color") == originalColor) {
$("." + this.boxClassName).css("background-color", this.changeColor);
} else {
$("." + this.boxClassName).css("background-color", this.originalColor);
}
}.bind(this));
First of all, you can bind
function to get proper this
首先,你可以
bind
功能,以获得适当的this
$(this.boxClassName).click(function (){
// this.boxClassName is now defined here
if ($("." + this.boxClassName).css("background-color") == originalColor) {
$("." + this.boxClassName).css("background-color", this.changeColor);
} else {
$("." + this.boxClassName).css("background-color", this.originalColor);
}
}.bind(this));
Also consider using slightly different approach 也考虑使用略有不同的方法
function Box(boxClassName, originalColor, changeColor) {
var changedBg = false, div = $('<div />'), handler = function ( e ) {
$(this).css('background-color', changedBg ? originalColor : changeColor)
changedBg = !changedBg
}
div.addClass(boxClassName)on('click', handler).appendTo(document)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.