简体   繁体   中英

How to wrap some html with a CDATA tag in jquery?

How do you wrap some html in a element in a CDATA tag so it become 'sterilized' so to speak...

I've tried...

$(this).html( "<![CDATA[" + $(this).html() + "]]>" );

and

$(this).wrap("CDATA");

The first wraps it in save html entities so it's not a CDATA tag. And the second doesn't work.

Thanks.

Browsers only support CDATA markers in XHTML, so you need to:

  1. Write XHTML.
  2. Serve it as Content-Type: application/xhtml+xml so the browser doesn't push it through an HTML parser anyway.

(I only tested the above on Chrome, you can see the results ).

You'd be better off getting the browser to treat it as text instead of as HTML:

$(this).text( $(this).html() );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM