javascript - Injecting a tag having an attribute with colon in it using jquery - Stack Overflow

I'm trying to inject below span in a div using jquery.HTML:<span epub:type="pagebreak&quo

I'm trying to inject below span in a div using jquery.

HTML:
<span epub:type="pagebreak" id="pagePrefix" title="1"></span>

JS:
$('div').html('<span epub:type="pagebreak" id="pagePrefix" title="1"></span>');

and getting the following error, SyntaxError: DOM Exception 12

any workaround this?

I'm trying to inject below span in a div using jquery.

HTML:
<span epub:type="pagebreak" id="pagePrefix" title="1"></span>

JS:
$('div').html('<span epub:type="pagebreak" id="pagePrefix" title="1"></span>');

and getting the following error, SyntaxError: DOM Exception 12

any workaround this?

Share Improve this question edited Apr 9, 2013 at 13:24 Blazemonger 93.1k27 gold badges145 silver badges181 bronze badges asked Apr 5, 2013 at 13:04 codef0rmercodef0rmer 10.5k9 gold badges54 silver badges78 bronze badges 1
  • change epub:type to epub-type.. or to be strict with html use data-epub-type – Vishal Sharma Commented Apr 5, 2013 at 13:06
Add a ment  | 

3 Answers 3

Reset to default 5 +100

jQuery uses innerHTML when you pass serialized DOM nodes to $('div').html(). This works fine as long as your DOCTYPE is any flavor of html. However, with the DOCTYPE of xhtml your serialized DOM nodes must clear some additional cases before being inserted into the document. According to W3, a serialized DOM node containing an Attr node, Text node, CDATASection node, Comment node, or ProcessingInstruction node whose data contains characters that are not matched by the XML Char production (including U+003A COLON ":") must raise an INVALID_STATE_ERR exception.

The W3 also specifies the algorithm that user agents must run when setting the innerHTML DOM attribute on HTMLElements and HTMLDocuments in an XML(XHTML) context. Step 2 in that algorithm is:

If the innerHTML attribute is being set on an element, the user agent must feed the parser just created the string corresponding to the start tag of that element, declaring all the namespace prefixes that are in scope on that element in the DOM, as well as declaring the default namespace (if any) that is in scope on that element in the DOM.

The user agent doesn't know that you've specified the epub namespace on a parent element because the current context is outside the root context. Therefore you need to specify the namespace for the serialized DOM nodes when you append to the DOM.

$('div').html('<span xmlns:epub="http://www.idpf/2007/ops" 
        epub:type="pagebreak" id="pagePrefix" title="1"></span>');

jsFiddle here.

Escape the colon:

$('div').html('<span epub\:type="pagebreak" id="pagePrefix" title="1"></span>');
$('div').append('<span />').attr('epub\:type', 'pagebreak').attr('id', 'pagePrefix').attr('title', '1');

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信