JQuery. parseHTML () function details

Source: Internet
Author: User
This article mainly introduces the jQuery. parseHTML () function, which is used to parse HTML strings into corresponding DOM node arrays. For more information, see definitions and usage.

$. ParseHTML () is used to parse an HTML string into a corresponding DOM node array.

Note:

1. This function will use the native DOM element creation function to convert the HTML string into a collection of DOM elements. you can insert these DOM elements into the document.

2. If the context parameter is not specified, or the parameter is null or undefined, the current document is used by default. If the created DOM element is used in another document, for example, iframe, you should specify the document Object of the iframe.

Security considerations: Most jQuery APIs allow HTML strings to include running scripts in HTML. JQuery. parseHTML () does not run scripts in the parsed HTML, unless you explicitly set the keepScripts parameter to true. However, most environments can still execute scripts indirectly, for example, through attributes. The caller should avoid this and clear or escape any untrusted input from sources such as URLs and cookies to prevent this situation. For future compatibility considerations, when the keepScripts parameter is omitted or false, the caller should not rely on any running script content.

Syntax

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

Create an array Dom node using an HTML string and insert it into a p

Content:

Script $ (function () {var $ log = $ ("# log"), str = "hello,My name isJQuery. ", html = $. parseHTML (str), nodeNames = []; // Add the parsed HTML $ log. append (html); // set the name of the HTML node that has been parsed $. each (html, function (I, el) {nodeNames [I] ="
  • "+ El. nodeName +"
  • ";}); // Insert Node name $ log. append (" Node Names: "); $ ("
      "). Append (nodeNames. join (" "). appendTo ($ log) ;}) script

      Example & Description

      The following is the jQuery Sample Code related to the jQuery. parseHTML () function to demonstrate the specific usage of the jQuery. parseHTML () function:

      // "<\/Script>" must pass \ to escape/. Otherwise, JS will think that it has reached the end of the script. var html = 'hello,CodePlayer
      Related Article

      Contact Us

      The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

      If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

      A Free Trial That Lets You Build Big!

      Start building with 50+ products and up to 12 months usage for Elastic Compute Service

      • Sales Support

        1 on 1 presale consultation

      • After-Sales Support

        24/7 Technical Support 6 Free Tickets per Quarter Faster Response

      • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.