【问题标题】:Selecting a node in Raphael/JavaScript在 Raphael/JavaScript 中选择一个节点
【发布时间】:2010-09-27 21:31:03
【问题描述】:

以下代码大部分都有效(感谢几天前的一个好答案!)除了最后一点:

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>

【问题讨论】:

  • 你怎么知道它不起作用?例如。你有错误吗?如果是这样,它说明了什么?还是默默地失败了?如果是这样,您希望它做什么,而它没有做什么?
  • 它只是默默地失败了。我希望它会导致当前画布(canvas_things1)淡出,而另一个画布淡入。相反,什么也没有发生(画布不会淡出)。
  • 实际上,当 'square' 不是路径数组,而只是路径时,它曾经可以工作:things.square = RM.path("...");和 things.square.node.setAttribute("id","clicker");.

标签: javascript dom raphael


【解决方案1】:

id 属性设置正确。

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

注意,我不得不撕掉你的 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
};

【讨论】:

    【解决方案2】:

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

    1. 循环遍历things.square 中的每个路径元素并将点击功能应用于每个元素(将无法使用id 属性)。
    2. 使用 group add-on 将所有路径放在 SVG 中的 &lt;g&gt; 元素中,然后将您的函数应用于该元素。

    【讨论】:

    • things.square[1].node 是单个 DOM 元素。那部分代码实际上是有效的。 for 循环不起作用。
    猜你喜欢
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    相关资源
    最近更新 更多