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.