How to select a raphael svg tag with jQuery?

天大地大妈咪最大 提交于 2020-01-03 04:58:49

问题


I'm quite new to Raphael, and quite lost too in its documentation...

I'd like to manipulate a Raphael object through jQuery:

$(".handle").hover(
               function() {
                     $("path[rel='"+$(this).attr('rel')+"']").addClass("pathhover");
               }, 
               function() {
                    $("path").removeClass("pathhover");
               }
            );

Path is a Raphael-generated path, and jQuery does not seem to be able to select this svg tag.

Would you have an idea how to achieve this ?

Thanks ;)


回答1:


Here is a solution, thanks to jacktheripper help.

The raphael object can't be manipulated directly by jQuery; we need to use its reference to the DOM (its node).

Therefore, the jQuery handler needs the Raphael object name. In this example, I use the rel tag to store the name of the raphael object.

$(".handle").hover(
               function() {
                     var path=eval($(this).attr('rel'));
                     path.node.setAttribute("class","pathhover");
               }, 
               function() {
                    var path=eval($(this).attr('rel'));
                    path.node.removeAttribute("class","pathhover");
               }
            );



回答2:


Much easier to use the native hover methods from Raphael http://irunmywebsite.com/raphael/additionalhelp.php?q=raphaelevents Using node can yield incomplete results



来源:https://stackoverflow.com/questions/12337971/how-to-select-a-raphael-svg-tag-with-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!