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 中文网</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>


Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <div name="test" class="dv" id="dv">php 中文网</div> <script type="text/javascript"> var div = document.getElementById("dv"); var test = div.innerHTML = "php.cn"; alert(test); </script> </body> </html>
submitReset Code