简体   繁体   中英

How to embed SVG styling with javascript?

I'm trying to embed a CSS styling into a SVG content (like in this example ) using JavaScript. The SVG itself is embedded into a HTML5 file. The purpose of style embedding is to save a SVG online created content into a standalone *.svg file.

Here is the first tested content: http://jsfiddle.net/3L8a2oxy/

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SVG embedded styling</title>
</head>

<body>
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
        <defs></defs>
        <circle  cx="50" cy="50"  r="25"/>
    </svg>

    <script>
        var svg = document.getElementsByTagName("svg")[0];
        var defs = svg.getElementsByTagName('defs')[0];
        var style = document.createElement('style');
        defs.appendChild(style);
        style.setAttribute('type', 'text/css');

        var node = document.createTextNode('<![CDATA[circle { fill: red; }]]>');
        style.appendChild(node);

    </script>
</body>

</html>

In that example styling doesn't work:

在此处输入图片说明

and Firefox (v 34.0.5) gives the error:

Selector expected. Rulset ignore due to bad selector.

However, if you make a separate html file with this code, save it, and reopen in a browser, the styling can be seen, ie it is parsed by browser correctly.

If one changes the style code as to:

var node = document.createTextNode('circle { fill: red; }');

在此处输入图片说明

then styling works online with no errors ( http://jsfiddle.net/8grf344g/ ). But it's not what the specifications told us to do. I can save the SVG part into a file in then open it in the browser, and it appears to be styled correctly, but I'm not sure how it will behave in other applications.

So, the question is in the subject.

PS. There is another issue for which I haven't found any solution yet. If you try to merge style data with style tag as follows:

var node = document.createTextNode('<style type=\'text/css\'><![CDATA[circle { fill: red; }]]></style>');
defs.appendChild(node);

then to goes into the document encoded:

&lt;style type='text/css'&gt;&lt;![CDATA[circle { fill: red; }]]&gt;&lt;/style&gt;

I know it happens for the inner content of html elements, but why it doesn't happen in the above first case?

PS Since the answers to the subject kindly provided by the community point to a right direction, but don't give a working example, I put one to jsfiddle ( http://jsfiddle.net/26wfwa12/1/ ). Feel free to use it and comment.

You've two main bugs. Firstly, you need to create the style element in the SVG namespace ie

var style = document.createElementNS('http://www.w3.org/2000/svg', 'style');

Secondly you can't use <![CDATA[ in html it's an xhtml only thing so you want

var node = document.createTextNode('circle { fill: red; }');

Finally (and this bit is optional), the text/css attribute can be omitted if you wish. All of which gives you this

The specifications certainly do not tell you to use <![CDATA[ with html, but they do tell you to use it with xhtml and xml and SVG at one time was an xml only language. If you intend to have standalone svg files (which will be served as image/svg+xml) then you may need to put the <![CDATA[ back in.

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