gpt4 book ai didi

javascript - 在 Raphael/JavaScript 中选择节点

转载 作者:行者123 更新时间:2023-11-28 02:50:45 25 4
gpt4 key购买 nike

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

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>

最佳答案

id 属性设置正确。

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

注意,我必须删除你的 for 循环。这是因为自执行匿名函数由于使用 undefined variable 调用而导致运行时错误。

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
};

关于javascript - 在 Raphael/JavaScript 中选择节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3808051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com