Latest web development tutorials

jQuery.parseHTML () method

jQuery Misc Methods jQuery Misc Methods

Examples

Use an HTML string to create an array of Dom node, and insert it into a div

<Div id = "log"> <h3 > Content: </ h3> </ div> <script> $ (function () { var $ log = $ ( "#log" ), Str = "hello, < b> my name is </ b> jQuery.", Html = $. ParseHTML ( str ), NodeNames = []; // add parsed HTML $ Log. Append ( html ); // Collection is HTML node name resolution $. Each ( html, function ( i, el ) { nodeNames [i] = "<li >" + el nodeName + "</ li>";.}); // insert node name $ Log. Append ( "<H3> Node Names: < / h3>" ); $ ( "<Ol> </ ol> " ). Append ( nodeNames. join ( ' " ) ). AppendTo ($ log );}) </ Script>

try it"

Definition and Usage

$ .parseHTML () Function is used to parse the HTML string for the DOM node corresponding to the array.

Note: 1. This function will create a function using native HTML DOM element to convert a string to a set of DOM elements, you can use these DOM elements into the document.
2. If you do not specify a context parameter, or the parameter is null or undefined, it defaults to the current document. If the DOM element created to another document, such as iframe, you should specify the iframe's document object.
Security considerations: Most jQuery API allow HTML string containing the run script in HTML. jQuery.parseHTML () does not run the script in parsed HTML, unless you explicitly specified as a parameter keepScripts true. However, in most environments can still execute the script indirectly, for example: by Attributes. Callers should avoid doing, and clean or escape, such as URL, any untrusted input from sources such as cookie and thus prevent the emergence of such a situation. For future compatibility, when the parameter keepScripts be omitted or false, the caller should not rely on any of this content is the ability to run scripts.


grammar

$.parseHTML( htmlString [, context ] [, keepScripts ] )

参数 描述
htmlString String类型 需要解析并转为DOM节点数组的HTML字符串
context Element类型 指定在哪个Document中创建元素,默认为当前文档的document
keepScripts Boolean类型 指定传入的HTML字符串中是否包含脚本,默认为false


jQuery Misc Methods jQuery Misc Methods