<p>jQuery.parseHTML( data [, context ] [, keepScripts ] )Returns: Array</p><p>Description: Parses a string into an array of DOM nodes.</p><p>version added: 1.8jQuery.parseHTML( data [, context ] [, keepScripts ] )</p><p>data</p><p>Type: String</p><p>HTML string to be parsed</p><p>context (default: document)</p><p>Type: Element</p><p>Document element to serve as the context in which the HTML fragment will be created</p><p>keepScripts (default: false)</p><p>Type: Boolean</p><p>A Boolean indicating whether to include scripts passed in the HTML string</p><p>jQuery.parseHTML uses native methods to convert the string to a set of DOM nodes, which can then be inserted into the document. These methods do render all trailing or leading text (even if that&#39;s just whitespace). To prevent trailing/leading whitespace from being converted to text nodes you can pass the HTML string through jQuery.trim.</p><p>By default, the context is the current document if not specified or given as null or undefined. If the HTML was to be used in another document such as an iframe, that frame&#39;s document could be used.</p><p>As of 3.0 the default behavior is changed. If the context is not specified or given as null or undefined, a new document is used. This can potentially improve security because inline events will not execute when the HTML is parsed. Once the parsed HTML is injected into a document it does execute, but this gives tools a chance to traverse the created DOM and remove anything deemed unsafe. This improvement does not apply to internal uses of jQuery.parseHTML as they usually pass in the current document. Therefore, a statement like $( &quot;#log&quot; ).append( $( htmlString ) ) is still subject to the injection of malicious code.</p><p>Security Considerations</p><p>Most jQuery APIs that accept HTML strings will run scripts that are included in the HTML. jQuery.parseHTML does not run scripts in the parsed HTML unless keepScripts is explicitly true. However, it is still possible in most environments to execute scripts indirectly, for example via the &lt;img onerror&gt; attribute. The caller should be aware of this and guard against it by cleaning or escaping any untrusted inputs from sources such as the URL or cookies. For future compatibility, callers should not depend on the ability to run any script content when keepScripts is unspecified or false.</p><p>Example:</p><p>Create an array of DOM nodes using an HTML string and insert it into a div.</p><pre class="brush:html;toolbar:false">&lt;!doctypehtml&gt; &lt;html&gt; &lt;head&gt; &lt;metacharset=&quot;utf-8&quot;&gt; &lt;title&gt;jQuery.parseHTMLdemo&lt;/title&gt; &lt;scriptsrc=&quot;https://code.jquery.com/jquery-1.10.2.js&quot;&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;divid=&quot;log&quot;&gt; &lt;h3&gt;Content:&lt;/h3&gt; &lt;/div&gt; &lt;script&gt; var$log=$(&quot;#log&quot;), str=&quot;hello,&lt;b&gt;mynameis&lt;/b&gt;jQuery.&quot;, html=$.parseHTML(str), nodeNames=[]; //AppendtheparsedHTML $log.append(html); //GathertheparsedHTML&#39;snodenames $.each(html,function(i,el){ nodeNames[i]=&quot;&lt;li&gt;&quot;+el.nodeName+&quot;&lt;/li&gt;&quot;; }); //Insertthenodenames $log.append(&quot;&lt;h3&gt;NodeNames:&lt;/h3&gt;&quot;); $(&quot;&lt;ol&gt;&lt;/ol&gt;&quot;) .append(nodeNames.join(&quot;&quot;)) .appendTo($log); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt;</pre>
RangeMem:207.55 KB
返回顶部 留言