【问题标题】:Change first element from SVG/XML从 SVG/XML 更改第一个元素
【发布时间】:2015-05-07 14:53:30
【问题描述】:

我有一个 svg 文件,例如以:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<!-- Généré par Microsoft Visio 11.0, SVG Export, v1.0 A_QI_QCompliance_ICompliance_AAfr.svg Compliance -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events"
        xmlns:v="http://schemas.microsoft.com/visio/2003/SVGExtensions/" width="11.6872in" height="8.79076in"
        viewBox="0 0 841.476 632.935" xml:space="preserve" color-interpolation-filters="sRGB" class="st36">

使用 javascript,我想更改第一行:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

进入:

<?xml-stylesheet version="1.0" encoding="UTF-8" standalone="no" type="text/css" href="../../style/css/customSVG.css"?> 

我不知道如何访问这个元素/节点来更改他的标记名/属性。

firstChild&lt;!DOCTYPE ...&gt;标签,documentElement&lt;svg&gt;...&lt;/svg&gt;标签

我无法创建 xml 或 ?xml 类型的元素。

【问题讨论】:

    标签: javascript xml svg


    【解决方案1】:
    var nodeDoctype = document.implementation.createDocumentType(
     'xml',
     'xml-stylesheet version="1.0" encoding="UTF-8" standalone="no" type="text/css" href="../../style/css/customSVG.css'
    );
    if(document.doctype) {
        document.replaceChild(nodeDoctype, document.doctype);
    } else {
        document.insertBefore(nodeDoctype, document.childNodes[0]);
    }
    

    根据您的评论更新:

    可以使用 JS 更改 doctype 以启用兼容性查看(如此处​​:http://www.webmasterworld.com/forum91/4856.htm),但这是一个非常讨厌的 hack,不推荐。理想情况下,您可以在服务器端执行此操作。所以有一个 doctype js 参数,然后重新加载页面:

    window.location = window.location+"?doctype=newdoctype"
    

    这会导致页面重新加载,这可能不适合您,但这是最安全的方式。

    【讨论】:

    • createDocumentType 函数中似乎缺少一个属性。并且不是要更改 标记,而不是 吗?
    猜你喜欢
    • 1970-01-01
    • 2015-08-18
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 2012-02-21
    相关资源
    最近更新 更多