开发者

How to create an attribute in SVG using JavaScript?

I have a script in my application that hides g elements in an SVG that have specific ID values, but this only works if the g element has a visibility attribute. However, the SVGs I'm using do not have a visibility attribute on the g elements and I'm not in control of the source. Therefore, I need to find a way to dynymically add the visibility attribute when the parent HTML page is loaded.

I would like the script to create the visibility attribute on all g elements that are children of <g id="Callouts">. For instance, the final code would look something like this:

<g id="Callouts">
  <g id="Callout1" visibility="visible">...</g>
  <g id="Callout2" visibility="visible">...</g>
</g>

I've looked all over for examples where attributes are added to the SVG structure, but haven't been able to find anything yet. It also doesn't help that I'm a complete novice when it comes to JavaScript. Does anyone know how to do this?

UPDATE: I coupled Digital Plane's suggested code with the code I'm using to access the SVG document. The resulting function is below. This is supposed to show every g element under <g id="Callouts">. However, I keep getting an "object not supported" error on the for loop.

function displayOnload (svgName) {
    var svgEmbed = document.embeds[svgName];
    if (typeof svgEmbed.getSVGDocument != 'undefined') {
        var svgDocument = svgEmbed.getSVGDocument();
        var parentElement = svgDocument.getElemen开发者_如何学JAVAtById('Callouts');
        if (parentElement != null) {
            var childElements = parentElement.getElementsByTagname('g');
            for (var i=0; i < childElements.length; i++) {
                childElements[i].setAttribute('visibility', 'visible');
            }
        }
    }
}

Please forgive my ignorance about JavaScript, but what am I doing wrong here?

UPDATE: Here is an example of my HTML code.

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:svg="http://www.w3.org/2000/svg">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>Drop Bar assembly (2328775)</title>
  <link rel="stylesheet" type="text/css" href="Content.css" />
  <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
  <script type="text/javascript">
    function displayOnload (svgName) {
      var svgEmbed = document.embeds[svgName];
      if (typeof svgEmbed.getSVGDocument != 'undefined') {
        var svgDocument = svgEmbed.getSVGDocument();
        var parentElement = svgDocument.getElementById('Callouts');
        var childElements = parentElement.getElementsByTagName('g');
        for (var i=0; i &lt; childElements.length; i++) {
          childElements[i].setAttribute('visibility', 'hidden');
        }
      }
     }
  </script>
  </head>
  <body onload="displayOnload('SVG')">
  ...
  </body>
</html>


You should be able to use setAttribute, with something like this:

element.setAttribute("visibility", "visible");

If you want all g elements that are children of <g id="Callouts">, do this on document load:

var element = document.getElementById("Callouts");
var elements = element.getElementsByTagName("g");
for(var i = 0; i < elements.length; i++)
  elements[i].setAttribute("visibility", "visible");


No, you must use setAttributeNS(null, 'visibility', 'visible);

setAttribute might not work on/in svg on some browsers.

Or to be more blunt: setAttribute is DOM1 setAttributeNS is DOM2 Svg has name spaces, svg is xml so setAttributeNS is for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜