Home >Web Front-end >JS Tutorial >How to get the source code of dom object in javascript
Two methods to get the source code of a DOM object in JavaScript are: 1. Use the innerHTML attribute to get the HTML code contained in the element; 2. Use the outerHTML attribute to get the complete HTML code of the element and its sub-elements.
How to get the source code of the DOM object
In JavaScript, you can get the DOM object in the following two ways Source code:
1. innerHTML attribute
Use the innerHTML
attribute to get all the HTML code contained in the DOM element, including the code of its child elements.
<code class="javascript">const element = document.getElementById("myElement"); const sourceCode = element.innerHTML;</code>
2. outerHTML attribute
Use the outerHTML
attribute to get the complete HTML code of the DOM element, including itself and child elements.
<code class="javascript">const element = document.getElementById("myElement"); const sourceCode = element.outerHTML;</code>
Normally, the innerHTML
attribute is used to get the content inside the element, while the outerHTML
attribute is used to copy the entire structure of the element.
The above is the detailed content of How to get the source code of dom object in javascript. For more information, please follow other related articles on the PHP Chinese website!