简体   繁体   中英

Modifying SVG after it has been loaded

How would I modify an SVG file after it has been loaded by the browser, preferably through jquery? A simple example would be pressing a button and the color of the SVG element changes. Any documentation would help as well.

EDIT: This link helped a great deal: w3.org/Graphics/SVG/IG/resources/svgprimer.html#SVG_in_HTML

You can't "modify" SVG files (except by changing them on the server). SVG files define a collection of SVG objects, each of which can be identified with an ID, if you wish. These objects can be manipulated with JavaScript like you would any DOM element (eg setAttribute, etc). Check http://www.w3.org/TR/SVG11/types.html#BasicDOMInterfaces for the DOM interfaces. Notice that SVGElement extends Element, which is the basic DOM element type.

EDIT: simple example:

<html>
<body>
    <input type="button" onclick="doSVGThing()" value="change">
    <svg xmlns="http://www.w3.org/2000/svg"
        xmlns:xlink="http://www.w3.org/1999/xlink">
    <rect id="aRect" x="10" y="10" height="100" width="100"
            style="stroke:#ff0000; fill: #9999ff"></rect>
</svg>
    <script type="text/javascript">
        function doSVGThing() {
            var r = document.getElementById('aRect');
            r.setAttribute('style', 'stroke: #00ff00; fill: #99ff99');
        }
    </script>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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