Javascript Basics Tutorial: DOM and HTML
Change html content
innerHTML
Let’s write it below An example, the code is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <div name="test" class="dv" id="dv">php 中文網(wǎng)</div> <script type="text/javascript"> var div = document.getElementById("dv"); var test = div.innerHTML = "php.cn"; alert(test); </script> </body> </html>
As in the above code, first we get the element node through the id, then change the value and assign it to the variable test, and then output
Change html attributes, the code is as follows
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <img src="https://img.php.cn/upload/course/000/000/010/582eab68f0bdb633.jpg" name="image" id="image" width="50" height="50"> <script type="text/javascript"> //注釋這段代碼,看一下,是一張圖,運行之后,顯示的不是原圖,是另外一張圖 document.getElementById("image").src="https://img.php.cn/upload/course/000/000/010/582eab9aa05cd314.jpg"; </script> </body> </html>