Home  >  Article  >  Web Front-end  >  jquery method of getting and setting outerhtml_jquery

jquery method of getting and setting outerhtml_jquery

WBOY
WBOYOriginal
2016-05-16 16:10:381444browse

The example in this article describes how jquery obtains and sets outerhtml. Share it with everyone for your reference. The specific analysis is as follows:

During the development process, jQuery.html() is to obtain the html code under the current node, and does not include the code of the current node itself. However, sometimes we do need it, and we can use jQuery.prop("outerHTML") set up.

Many jQuery users are deeply confused about this issue. Why can't the outerHTML of the html element be directly set like in the DOM among the many convenient methods of getting and setting attributes?

Because there is a built-in attribute outerHTML in the native JS DOM (look at the case, JS is case-sensitive) to get the html code of the current node (including the current node), so you can use jQuery's prop() to get it to

1. jquery gets outerhtml

<div class="test"><p>hello,你好!</p></div>
<script>
$(".test").prop("outerHTML");
</script>

2. jquery setting outerhtml

$('.test').prop('outerHTML', '<input>');

I hope this article will be helpful to everyone’s jQuery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn