Jquery: Getting outerHtml code containing the current node _ jquery-js tutorial

Source: Internet
Author: User
JQuery.html () is to obtain the html code under the current node, does not contain the code of the current node itself. Later, the experiment found that there is a jQuery method to solve the problem. In the development process, jQuery.html () is to get the html code under the current node, does not contain the code of the current node itself, and sometimes we do need to find the jQuery api documentation, there is no way to get it.

We can see that some people pass through parent().html (). If the current element does not have a sibling element, it will not work if it does. Later, the experiment found that there is a jQuery method that can be solved, and it is very simple, as follows:

JQuery. prop ("outerHTML ");

The Code is as follows:


Hello!


Script
$ (". Test"). prop ("outerHTML ");
Script


Output result:

Hello!

Because the native js dom has a built-in attribute outerHTML (case sensitive, JS is case sensitive) to get the html code of the current node (including the current node ), so we can get it using jQuery's prop (). The attr () method is not available after the experiment. If you don't believe it, you can try it. Thank you.

Of course, some people also use jQuery's clone () function with append () to create a node with only one child element and then get the node's html. This is also feasible, but the code is cumbersome.

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.