更改SVG'style'元素
Change SVGs 'style' element in IE/Edge
我有一个SVG,它包含一些css。我想用javascript更改这个SVG中的style
元素,但在IE/Edge中你可以;t用CCD_ 3改变CCD_。相反,您需要.styleSheet.cssText
,但我无法让它在我的SVG中工作。
这适用于非IE浏览器,以获取和设置值:
var style = document.getElementById('example').contentDocument.querySelector("style");
style.innerHTML = style.innerHTML.replace('foo', 'bar')
SVG(简化):
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 540 494.08">
<defs>
<style id="SvgStyleSheet">
.someClass{}
</style>
<!-- Some more definitions -->
</defs>
<!-- SVG elements here -->
</svg>
如何获取和设置此SVG中的样式
事实证明,我非常非常接近.styleSheets.cssText
,不得不使用略有不同的方法。
为了让它在IE/Edge、Firefox、Android、Chrome和iPhone浏览器中继续工作,我不得不同时实现这两种功能。
var svgDOC = document.getElementById('idOfSVG').contentDocument;
style = svgDOC.styleSheets[0];
// IE/Edge
if( typeof style.cssText !== "undefined" ){
style.cssText = style.cssText.replace('foo', 'bar');
}
// Non IE/Edge:
else{
var style = svgDOC.querySelector("style");
style.innerHTML = style.innerHTML.replace('foo', 'bar');
}
注意:
值得注意的是,Firefox和其他浏览器在编写时会返回实际的innerHTML。如果没有换行符,则结果中没有换行符:.example1{boo:bar;}
然而,IE和Edge将返回解析后的结果。
.example1 {
boo: bar;
}
相关文章:
- CKEditor Widget-阻止编辑可编辑元素本身
- HEAD中LINK和STYLE元素的求值顺序
- 更改SVG'style'元素
- 为什么当我分配给element.style时,我的元素的样式没有改变
- 为包含 HTML 标记的字符串中的元素设置 Style 属性
- 无法更新返回的元素.style.left,为什么
- 使用 JavaScript 更改
- 是否可以通过nightwatch.js设置元素的Style属性的值?如果是,那么如何
- 在同一个元素上使用多个.style.transform
- 如何获取与元素SPAN Style With JavaScript相关的所有文本
- this.style.cursor='手',当我把鼠标悬停在元素上时,第一次没有工作
- Firefox忽略Javascript元素.style.backgroundPosition
- 元素未显示为obj.style.display='';
- Javascript:通过.style设置元素悬停时的颜色
- 当style在id/class css中定义时,获取/设置元素的style
- jQuery:有没有一种方法可以抓取HTML元素的外部CSS并附加到它的“style”上?属性
- 无法在 JS 中访问元素的 style.height
- 当使用 element.style = <一些 CSSStyleDeclaration> 时,会使元素消失
- 查找并替换子元素的style属性
- d3.style won't设置HTML元素的宽度