Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 拉斐尔节点选择_Jquery_Class_Selection_Raphael - Fatal编程技术网

Jquery 拉斐尔节点选择

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

我在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
完成的,但是这里我没有为每个
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');
     });