[英]Javascript: How to call object method on Raphael path?
我有一个JavaScript对象,其中包含许多Raphael路径。 (这些路径构成一堆饼图,所有饼图都绘制在同一张纸上。)我希望每个路径在单击时触发一个对象方法。 但是,当我单击路径时,出现“ Uncaught TypeError:undefined is not function”(Chrome for Mac OS)。 有谁知道如何做到这一点? 这是我的代码的摘要:
// Definition of PieChartStack object
function PieChartStack() {
this.setNodeTree = function (nodeTree) {
this.nodeTree = nodeTree;
...
this.performInitialSetup();
}
this.performInitialSetup = function() {
...
var paper = Raphael("holder", "100%", "100%");
...
paper.customAttributes.segment = function (x, y, r, a1, a2) {
...
return {
path: [["M", x, y], ["l", r * Math.cos(a1), r * Math.sin(a1)], ["A", r, r, 0, +flag, 1, x + r * Math.cos(a2), y + r * Math.sin(a2)], ["z"]],
fill: "hsb(" + clr + ", .75, .8)"
};
};
this.handleSliceTap = function(chartIndex, sliceIndex) {
console.log(chartIndex + " , " + sliceIndex);
}
for (var chartIndex = 0; chartIndex < this.pieCharts.length; chartIndex++) {
...
for (sliceIndex = 0; sliceIndex < sliceCount; sliceIndex++) {
...
var path = paper.path().attr({segment: [this.centerX, this.centerY, 1, start, start + val], stroke: "#fff"});
// PROBLEM HERE ======================================
path.click(
function (e) {
this.handleSliceTap(chartIndex, sliceIndex);
}
);
//====================================================
}
}
}
return this;
}
Teemu知道了-在这种情况下,“ this”是路径,而不是PieChartStack。 通过创建一个新的var来解决:var self = this-然后像这样做:
self.handleSliceTap(chartIndex, sliceIndex);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.