简体   繁体   English

通过JavaScript或JQuery处理SVG文件(对象)中的元素

[英]Manipulate elements in SVG file (object) by JavaScript or JQuery

I have a challenge of manipulating SVG file (object) in html code. 我在用html代码处理SVG文件(对象)方面遇到了挑战。 There are solutions in Snap, Raphael but I need to do it directly by either JavaScript or JQuery. Snap,Raphael中有解决方案,但我需要直接通过JavaScript或JQuery来完成。 That's what I have so far: 到目前为止,这就是我所拥有的:

JS: JS:

<object id="testSVG" data="image_library/grandstaff_drawing_only.svg"       
type="image/svg+xml" height=100% width=100%">
<img src="image_library/alto-clef.png" />
</object>

 <script>

window.onload=function() {
// Get the Object by ID
var a = document.getElementById("testSVG");
// Get the SVG document inside the Object tag
var svgDoc = a.contentDocument;
// Get one of the SVG items by ID;
var svgItem = svgDoc.getElementById("path3380");
// Set the colour to something else
//svgItem.setAttribute("stroke", "red");
svgItem.style.stroke = "#ff0000";
};

</script>

JQuery: JQuery的:

<object id="testSVG" data="image_library/grandstaff_drawing_only.svg"    
type="image/svg+xml" height=100% width=100%">
<img src="image_library/alto-clef.png" />
</object>


<script>

window.onload=function() {
var svgDoc = $(“#testSVG”)[0].contentDocument; 
$(“#path3380”, svgDoc).css(“stroke”, “red”); 

};

</script>

Thank you!!! 谢谢!!!

Normally I use D3.js library when I want to create or manipulate SVG graphics by javascript alone. 通常,当我想单独使用javascript创建或处理SVG图形时,我会使用D3.js库。 http://d3js.org/ http://d3js.org/

Unlike libs like Raphaeljs, which polyfills, D3 provides a javascript API for direct manipulation of SVG elements within the browser in real time. 与像Raphaeljs这样的polyfill库一样,D3提供了一个JavaScript API,用于在浏览器中实时直接操作SVG元素。

D3 is Not a SVG Polyfill: Unlike Raphaël, which provides polyfill for SVG on browsers that do not support SVG. D3不是SVG Polyfill:与Raphaël不同,Raphaël在不支持SVG的浏览器上为SVG提供了polyfill。 D3 manipulates SVG directly, without any abstraction layer. D3直接操作SVG,而无需任何抽象层。 Your browser needs to support SVG for D3 to work properly. 您的浏览器需要支持SVG才能使D3正常工作。 ( source ) 来源

For example, here's a demo I made from a D3 tutorial that constructs and manipulates SVG elements to create a live javascript / svg driven clock using the Javascript native new Date() function: http://jsfiddle.net/2jogwx6x/1/ 例如,这是我从D3教程中获得的一个演示,该演示使用Javascript原生的new Date()函数构造和处理SVG元素以创建实时javascript / svg驱动的时钟: http : //jsfiddle.net/2jogwx6x/1/

The D3.js strategy will likely work for your purposes and the elem selection method is similar to jQuery's CSS selection method but working directly with dom nodes, but for it to work you would need to directly embed your SVG data into the DOM like: D3.js策略可能会满足您的目的,并且elem选择方法类似于jQuery的CSS选择方法,但直接与dom节点一起使用,但是要使其正常工作,您需要将SVG数据直接嵌入到DOM中,例如:

<!-- simple rectangle - replace this with your real svg data -->
<svg width="400" height="110">
    <rect width="300" height="100" style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)">
</svg>

Instead of using <object id="testSVG" data="image_library/grandstaff_drawing_only.svg" type="image/svg+xml" height=100% width=100%"> <object id="testSVG" data="image_library/grandstaff_drawing_only.svg" type="image/svg+xml" height=100% width=100%">使用<object id="testSVG" data="image_library/grandstaff_drawing_only.svg" type="image/svg+xml" height=100% width=100%">

To manipulate SVG data in the DOM on the fly it has to be a part of the DOM and not opening / writing to / closing an external file. 要在DOM中动态处理SVG数据,它必须是DOM的一部分,并且不能打开/写入/关闭外部文件。

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

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