Jquery 拉斐尔节点选择
我在Raphael画布上自动创建了许多矩形,为简单起见,此代码只使用了两个:Jquery 拉斐尔节点选择,jquery,class,selection,raphael,Jquery,Class,Selection,Raphael,我在Raphael画布上自动创建了许多矩形,为简单起见,此代码只使用了两个: for (i = 0; i < 2; ++i) { var square = paper.rect(0 + 100*i, 0, 70, 70); square.node.setAttribute('id', '_' + i); square.node.setAttribute('class', 'foo'); } 从我读到的所有内容来看,这是使用.node完成的,但是这里我没有为每个re
for (i = 0; i < 2; ++i) {
var square = paper.rect(0 + 100*i, 0, 70, 70);
square.node.setAttribute('id', '_' + i);
square.node.setAttribute('class', 'foo');
}
从我读到的所有内容来看,这是使用.node
完成的,但是这里我没有为每个rect
单独的变量,因为square
在for()
循环的每次迭代中都被覆盖
一种方法是将所有矩形推送到一个数组中,如下所示:
squares = [];
for (i = 0; i < 2; ++i) {
var square = paper.rect(0 + 100*i, 0, 70, 70);
square.node.idx = i;
square.node.setAttribute('class', 'foo');
squares.push(square);
}
但是。。。我仍然不知道如何通过通用函数change\u class()。。。我需要像这样的东西:
function change_class() {
//something here
}
$('rect').click(function() {
change_class(); // the click function should pass "$(this)" to change_class ?
});
什么是正确的jQuery+Raphael方法
提前感谢,,
Adrian如果要单击框本身来更改其颜色,不需要jQuery,可以使用Raphael的内置事件方法,并将矩形引用为this
,如下所示:
rect.click( function () {
this.node.setAttribute('class', 'newClass');
});
$('rect').click(function() {
change_class(); // the click function should pass "$(this)" to change_class ?
});
rect.click( function () {
this.node.setAttribute('class', 'newClass');
});