在 Raphael/JavaScript 中选择节点

发布于 2024-09-25 00:26:13 字数 2501 浏览 0 评论 0原文

下面的代码大部分都可以工作(感谢几天前的一个很好的答案!),除了最后一点:

things.square[1].node.setAttribute("id","clicker");
$("#clicker").click(function(){
    $("#canvas_things1").fadeOut();
    $("#canvas_things2").fadeIn();
});

我认为问题是这一行:

things.square[1].node.setAttribute("id","clicker");

我本以为 square[1].node 可以工作,但它看来不是。有人能发现出了什么问题吗?

<script type="text/javascript" charset="utf-8">
    $("document").ready(function() {
        var RM  = Raphael("canvas", 1000, 500);

        var attr = {     // for the visible shapes
            fill: "#bbb",      "fill-opacity": 1,
            stroke: "#222",    "stroke-width": 0.3,
        };
        var attr2 = {    // for the invisible hovering areas
            fill: "green",     "fill-opacity": 0.0,
            stroke: "red",     "stroke-width": 0,
        };
        var things = {};
        /* Square */    things.square     = [ RM.path("m 154.21525,71.431259 74.32805,0 0,70.496711 -74.32805,0 0,-70.496711 z").attr(attr),
                                              RM.path("m 271.25132,77.933263 58.07304,0 0,56.409037 -58.07304,0 0,-56.409037 z").attr(attr2)   ];
        /* Triangle */  things.triangle   = [ RM.path("m 154.02932,222.44063 36.78089,-58.23641 34.48208,58.2364 -71.26297,1e-5").attr(attr),
                                              RM.path("m 271.25132,165.71032 58.07304,0 0,56.40903 -58.07304,0 0,-56.40903 z").attr(attr2)   ];
        for (var shape in things) {
            shape[0].color = Raphael.getColor();
            (function (shape, sh) {
                shape[1][0].onmouseover = function () {
                    shape[0].animate({fill:shape[0].color, stroke:"#ccc"}, 500);
                    document.getElementById(sh)[0].style.display = "block";
                    shape[0].toFront();   R.safari();
                };
                shape[1][0].onmouseout = function () {
                    shape[0].animate({fill:"#bbb", stroke:"#222"}, 500);
                    document.getElementById(sh)[0].style.display = "none";
                    shape[0].toFront();   R.safari();
                };
            })(things[sh], sh);
        } // end for every member of things

        things.square[1].node.setAttribute("id","clicker");
        $("#clicker").click(function(){
            $("#canvas_things1").fadeOut();
            $("#canvas_things2").fadeIn();
        });

    }); // end the document ready function
</script>

The following code mostly works (thanks to a good answer a couple of days ago!) all except for the last little bit:

things.square[1].node.setAttribute("id","clicker");
$("#clicker").click(function(){
    $("#canvas_things1").fadeOut();
    $("#canvas_things2").fadeIn();
});

I think the problem is this line:

things.square[1].node.setAttribute("id","clicker");

I would have thought that square[1].node would work, but it seems not. Can someone spot what's wrong?

<script type="text/javascript" charset="utf-8">
    $("document").ready(function() {
        var RM  = Raphael("canvas", 1000, 500);

        var attr = {     // for the visible shapes
            fill: "#bbb",      "fill-opacity": 1,
            stroke: "#222",    "stroke-width": 0.3,
        };
        var attr2 = {    // for the invisible hovering areas
            fill: "green",     "fill-opacity": 0.0,
            stroke: "red",     "stroke-width": 0,
        };
        var things = {};
        /* Square */    things.square     = [ RM.path("m 154.21525,71.431259 74.32805,0 0,70.496711 -74.32805,0 0,-70.496711 z").attr(attr),
                                              RM.path("m 271.25132,77.933263 58.07304,0 0,56.409037 -58.07304,0 0,-56.409037 z").attr(attr2)   ];
        /* Triangle */  things.triangle   = [ RM.path("m 154.02932,222.44063 36.78089,-58.23641 34.48208,58.2364 -71.26297,1e-5").attr(attr),
                                              RM.path("m 271.25132,165.71032 58.07304,0 0,56.40903 -58.07304,0 0,-56.40903 z").attr(attr2)   ];
        for (var shape in things) {
            shape[0].color = Raphael.getColor();
            (function (shape, sh) {
                shape[1][0].onmouseover = function () {
                    shape[0].animate({fill:shape[0].color, stroke:"#ccc"}, 500);
                    document.getElementById(sh)[0].style.display = "block";
                    shape[0].toFront();   R.safari();
                };
                shape[1][0].onmouseout = function () {
                    shape[0].animate({fill:"#bbb", stroke:"#222"}, 500);
                    document.getElementById(sh)[0].style.display = "none";
                    shape[0].toFront();   R.safari();
                };
            })(things[sh], sh);
        } // end for every member of things

        things.square[1].node.setAttribute("id","clicker");
        $("#clicker").click(function(){
            $("#canvas_things1").fadeOut();
            $("#canvas_things2").fadeIn();
        });

    }); // end the document ready function
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〆一缕阳光ご 2024-10-02 00:26:13

id 属性设置正确。

用这个 jQuery 尝试一下(点击第一个红色方块)

注意,我不得不撕掉你的 for 循环。这是因为自执行匿名函数由于使用未定义的变量调用而导致运行时错误。

sh 从未定义,但在这里使用:

        })(things[sh], sh);                               // sh is never defined!
    } // end for every member of things

我会替换 for 循环,但我不明白你要做什么。

无论如何,我建议使用 Raphael 自定义事件处理程序 .hover()


PS:注意你不必要的昏迷(你有 4 个):

    stroke: "#222",    "stroke-width": 0.3,                 // <== Trailing comma
};

The id attribute is set properly.

Try it out with this jQuery (click on the 1st red square)

Notice, that I had to rip out your for loop. This is because the self executing anonymous function was causing a runtime error due to being called with undefined variables.

sh is never defined, but it is used here:

        })(things[sh], sh);                               // sh is never defined!
    } // end for every member of things

I would have replaced the for loop but I don't understand what you're trying to do.

At any rate, I would suggest using the Raphael custom event handler .hover().


PS: Watch your unnecessary comas (you have 4):

    stroke: "#222",    "stroke-width": 0.3,                 // <== Trailing comma
};
撑一把青伞 2024-10-02 00:26:13

我认为这是因为当您有多个路径时, things.square 不会映射到 SVG 中的单个元素。有两种可行的方法:

  1. 循环遍历 things.square 中的每个路径元素并对每个元素应用 click 函数(无法使用 id 属性然后)。
  2. 使用组插件将所有路径放入< SVG 中的 /code> 元素,然后将您的函数应用于该元素。

I think it's because things.square doesn't map to a single element in your SVG when you have multiple paths. There's two approaches that should work:

  1. Loop through each of the path elements in things.square and apply the click function to each of them (won't be able to use the id attribute then).
  2. Use a group add-on to put all your paths inside a <g> element in the SVG, then apply your function to that instead.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文