添加引用现有命名空间的XHTML元素

问题

给定定义了自定义命名空间的XHTML文档,

 

我如何使用jQuery(或vanilla JavaScript)添加一个元素,该元素具有引用该命名空间的属性,指定为字符串:

 var newElement = '

add me

';

你试过什么?

 var xhtml = '

two

'; try { $('div').append(xhtml); } catch(e){ alert(e); } // [FFv14] "An invalid or illegal string was specified" code: "12" // [Chromev21] Error: SYNTAX_ERR: DOM Exception 12 // [IE9] DOM EXception: SYNTAX_ERR (12) try { $('div')[0].innerHTML += xhtml; } catch(e){ alert(e); } // [FFv14] Works! // [IE9] Works! // [Chromev21] Error: SYNTAX_ERR: DOM Exception 12

测试页面( 托管 )

    Using JS to add element referencing custom namespace  

one

<![CDATA[ var xhtml = '

two

'; try { $('div').append(xhtml); } catch(e){ alert(e); } try { $('div')[0].innerHTML += xhtml; } catch(e){ alert(e); } ]]>

我能想到的最好的是

   Using JS to add element referencing custom namespace  

one

需要一些打包,但似乎适用于IE9,Chrome 21,FF 14和Opera 12。