简体   繁体   中英

Show source code of HTML element on click

I want to display the source code of an element on a web page inside a text box when the element is clicked. Is it possible to obtain the source code of an element when the element is clicked (using the onClick property), and then display the element inside the text box?

Yes, it certainly is possible. The following code is demonstrated here , and displays the source code when the text is clicked.

Here's an example with more elements on the page (which are all shown in the text box when clicked.)

<script type = "text/javascript">
function showSourceCode(toShow){
    sourceCode.value = toShow.innerHTML;
}
</script>

<p onclick = "showSourceCode(this);">
    <b>Show source code on click!</b>
</p>

<input type="text" id = sourceCode></input>


</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM