简体   繁体   English

需要使用jQuery,javascript或任何其他插件修复SVG DOM

[英]need to fix SVG DOM using jQuery , javascript or any other plugin

I'm creating a family tree using SVG, a small structure is given below. 我正在使用SVG创建一个家谱,下面给出了一个小结构。 I need help to add specific class(say 'selected') on mouse over on the class - "node", on every first "rect" of "g" which is the parents of the current hovered "node". 我需要帮助在鼠标上添加特定的类(比如'selected') - “节点”,在“g”的每个第一个“rect”上,它是当前悬停的“节点”的父节点。

$this.addClass('classname') is not working. $ this.addClass('classname')不起作用。 So I m using $this.attr('class','classname') 所以我使用$ this.attr('class','classname')

Impotent : I need a function like parents ( - in jQuery ) or similar methods to get all parent "g" of the current hovered "rect". 无能为力:我需要像父母一样的函数(在jQuery中)或类似的方法来获取当前悬停的“rect”的所有父“g”。

current work - click here 目前的工作 - 点击这里

A sample structure. 样本结构。

<svg style="width:100%;height:455px" id="svg_root" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
    <g id="viewport" >
        <g id="1">
            <rect class="node" y="0" x="0" width="160" height="35" />
            <text class="prof_name" y="14" x="34">Name</text>
            <g id="2">
                <rect class="node" y="40" x="30" width="160" height="35" />
                <text class="prof_name" y="54" x="64">Name</text>
                <g id="7">
                    <rect class="node" y="80" x="90" width="160" height="35" />
                    <text class="prof_name" y="94" x="94">Name</text>
                </g>
            </g>
        </g>
        <g id="18">
            <rect class="node" y="120" x="0" width="160" height="35" />
            <text class="prof_name" y="134" x="34">Name</text>
        </g>
    </g>
</svg>

I think jQuery is not friendly with SVG :( 我认为jQuery与SVG不友好:(

I would recommend you to use a jQuery plugin that lets you interact with an SVG canvas. 我建议你使用一个jQuery插件,让你与SVG画布进行交互。 Exactly this one http://keith-wood.name/svg.html (SVG DOM tab) 正是这个http://keith-wood.name/svg.html(SVG DOM选项卡)

The SVG DOM is different to the HTML DOM for which jQuery was designed. SVG DOM与为其设计jQuery的HTML DOM不同。 In particular, any attributes that can be animated are stored as objects instead of plain strings. 特别是,任何可以设置动画的属性都存储为对象而不是普通字符串。 This includes the class attribute. 这包括class属性。 Thus, jQuery's functions that work with classes don't work on SVG nodes. 因此,使用类的jQuery函数不适用于SVG节点。

To overcome this problem you can use the jQuery SVG DOM extension. 要解决此问题,您可以使用jQuery SVG DOM扩展。

<script type="text/javascript" src="jquery.svgdom.js"></script>

You could use raphael.js to add eventhandlers. 您可以使用raphael.js添加事件处理程序。 eg 例如

var paper = new Raphael( "id", 200, 200 );
var rect = paper.rect( 0, 0, 160, 35 );

rect.mouseover( function( ) {
    // do stuff
});

More information and demos here . 更多信息和演示在这里

A fiddle of how your family tree might work here 你的家谱如何在这里工作的小提琴

UPDATE UPDATE

Although Raphael.js does not provide functions to find the parent of a svg element you can still track your family tree by adding custom properties to raphael elements. 虽然Raphael.js不提供查找svg元素父级的函数,但您仍可以通过向raphael元素添加自定义属性来跟踪您的族树。

I have created an example here to show how you might achieve this. 我在这里创建了一个示例来说明如何实现这一目标。 Hopefully it helps. 希望它有所帮助。

This works for me . 这适合我。

$(window).ready(function(){
    $('#viewport .c_node').mouseenter(function(e) {
        paint_parent(e.target.parentNode);
    });
$('#viewport .c_node').mouseleave(function(e) {
        $('#viewport g').each(function(index, element) {
            $(element).attr('class',"");
        }); 
    });
});
function paint_parent(element){
        if(element.id!="viewport"){ // to the parent id viewport
        $('#'+element.id).attr('class',"cnode_parent");
        paint_parent(element.parentNode);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM