javascript - Dynamically change the CSS file of an external SVG file? - Stack Overflow

How to tell to the SVG image to use another CSS file ?A web page displays a SVG file.A button allows t

How to tell to the SVG image to use another CSS file ?

  • A web page displays a SVG file.
  • A button allows to switch between classic colors to high contrast on the whole web page including the SVG image.

Attempt

w.css (white background)

svg { background-color:white; }
path{ fill:none; stroke:black; stroke-width:8px; }

b.css (black background)

svg { background-color:black; }
path{ fill:none; stroke:white; stroke-width:10px; }

image.svg

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="w.css" title="classic" ?>
<?xml-stylesheet type="text/css" href="b.css" title="contrast" alternate="yes" ?>
<svg xmlns="" width="100%" height="100%">
   <path d="M150,100 H50 V300 H150 M250,300 H300" />
</svg>

example.html

<html>
<body>

<embed id="svg_image" src="image.svg" type="image/svg+xml" /> 

<script type="text/javascript">
var embed = document.getElementById("svg_image");
function change_css(file){
    var svgdoc = embed.getSVGDocument();
    var b = svgdoc.childNodes;
    for (var i=0; i<b.length; i++){
        var itm = b.item(i);
        itm.Data = "href='"+ file +"' type='text/css'" ;
    }
}
</script>

<input name="c" type="radio" onclick="change_css('b.css');">High contrast<br>
<input name="c" type="radio" onclick="change_css('w.css');" checked="yes">Classic

</body>
</html>

WEB SEARCH: No answer found in 2011

UPDATE: See also question about correctly structuring javascript, css, and svg
Maybe jQuery SVG (keith-wood.name)...

How to tell to the SVG image to use another CSS file ?

  • A web page displays a SVG file.
  • A button allows to switch between classic colors to high contrast on the whole web page including the SVG image.

Attempt

w.css (white background)

svg { background-color:white; }
path{ fill:none; stroke:black; stroke-width:8px; }

b.css (black background)

svg { background-color:black; }
path{ fill:none; stroke:white; stroke-width:10px; }

image.svg

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="w.css" title="classic" ?>
<?xml-stylesheet type="text/css" href="b.css" title="contrast" alternate="yes" ?>
<svg xmlns="http://www.w3/2000/svg" width="100%" height="100%">
   <path d="M150,100 H50 V300 H150 M250,300 H300" />
</svg>

example.html

<html>
<body>

<embed id="svg_image" src="image.svg" type="image/svg+xml" /> 

<script type="text/javascript">
var embed = document.getElementById("svg_image");
function change_css(file){
    var svgdoc = embed.getSVGDocument();
    var b = svgdoc.childNodes;
    for (var i=0; i<b.length; i++){
        var itm = b.item(i);
        itm.Data = "href='"+ file +"' type='text/css'" ;
    }
}
</script>

<input name="c" type="radio" onclick="change_css('b.css');">High contrast<br>
<input name="c" type="radio" onclick="change_css('w.css');" checked="yes">Classic

</body>
</html>

WEB SEARCH: No answer found in 2011
http://tech.groups.yahoo./group/svg-developers/message/56679

UPDATE: See also question about correctly structuring javascript, css, and svg
Maybe jQuery SVG (keith-wood.name)...

Share Improve this question edited May 23, 2017 at 12:30 CommunityBot 11 silver badge asked Sep 10, 2011 at 12:36 oHooHo 54.7k30 gold badges173 silver badges208 bronze badges 5
  • 1 I'm not sure what's the question here. Your approach is good, except the data attribute of processing instruction node should be lowercase. You also don't need two <?xml-stylesheet ?> PIs, creating just one and manipulating its data is enough. – duri Commented Sep 10, 2011 at 15:53
  • Yes, duri, you are right: I have lowercased itm.Data => itm.data and it works :-) – oHo Commented Sep 11, 2011 at 9:39
  • PS: firebug v1.8.2 do not detect this bug :-( ` ` => bug report – oHo Commented Sep 11, 2011 at 9:47
  • @duri : For bug report, I need to know whether firebug has to lowercase Data or firebug has to detect mistake in attribute case. – oHo Commented Sep 11, 2011 at 9:54
  • 2 This is not a bug. A node is an object so you're allowed to set any property, whether it has special function or not. Using itm.Data is the same as itm.someOtherCustomProperty - nothing will change if you set it, but you can do so. Javascript is case sensitive so data and Data properties are two unrelated things. – duri Commented Sep 11, 2011 at 11:00
Add a ment  | 

2 Answers 2

Reset to default 2

It's probably not the best idea to switch actual stylesheets. You're probably better off if you set a CSS class on a very high level and then switching that class with Javascript. Then you can put all the CSS rules in one file and just have to use selectors like (simplified):

<svg xmlns="http://www.w3/2000/svg" class="someclass">
    <style>
        .someclass .mypath { stroke: blue; }
        .someotherclass .mypath { stroke: red; }
    </style>
    <path d="M150,100 H50 V300 H150 M250,300 H300" class="mypath" />
</svg>

You know what I mean? It's like an if...else construct. If it's a descendant of someclass make it blue, otherwise make it red.

That said, I've heard that some browsers have problems with external stylesheets in SVG documents.

http://www.thesitewizard./javascripts/change-style-sheets.shtml happens to claim to explain how to enable/disable style sheets from javascript. Perhaps you can adapt it to SVG.

I did a quick experiment using firebug against a web page that embedded an SVG that contained a reference to an external CSS.

o=document.getElementsByTagName("object")
svg = o[0].getSVGDocument()
svg.styleSheets[0].disabled = true

It appears to work.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745177114a4615243.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信