我有这个问题,我被卡住了好几天。我正在尝试将svg附加到DOM。我在其中有定制的名称空间,当它们被添加到DOM中时,您尝试获取附加了它们的父元素的innerHTML属性,您会得到带有一些随机名称空间的它们。这种情况只在IE9中发生。
示例:
$(document).ready(function () {
var svg = '<svg xmlns="http://www.w3.org/2000/svg" ns:attr="val" />';
alert($("div").append(svg).html());
});输出将为:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:NS1="" NS1:a:b="val" />你知道怎么解决这个问题吗?我试图定义名称空间,但它不能再次工作。这是一个jsfiddle链接http://jsfiddle.net/RwNqk/3/
提前谢谢。
发布于 2012-08-15 23:42:20
首先,在JSFiddle中有一个超文本标记语言文档,而不是超文本标记语言。HTML没有自定义名称空间,只有XML/XHTML才有。
其次,您使用的是ns名称空间前缀,而没有定义该名称空间是什么。其他浏览器还能正常工作真是个奇迹。
第三,即使你解决了这些问题,你(不幸的) can't use jQuery to jam elements into the DOM using previously-defined namespace prefixes
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:foo="hello"><head>
<title>Using jQuery to add namespaced attribute</title>
</head><body>
<div><p foo:bar="yes">one</p></div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript"><![CDATA[
var xhtml = '<p foo:bar="no">two</p>';
alert($('div').html());
try{ $('div').append(xhtml); }
catch(e){ alert(e); }
]]></script>
</body></html>第一个警告显示自定义命名空间有效:
<p xmlns="http://www.w3.org/1999/xhtml" foo:bar="yes" xmlns:foo="hello">one</p>
第二个警报显示失败:
[Firefox] "An invalid or illegal string was specified" code: "12"
[Chrome] Error: SYNTAX_ERR: DOM Exception 12
[IE9] DOM EXception: SYNTAX_ERR (12)这与IE9或SVG无关。这主要与jQuery有关。(您可以在IE9和FF中设置DOM元素的.innerHTML,它将按预期工作,但不适用于Chrome。)
https://stackoverflow.com/questions/11970075
复制相似问题