dom基础6

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>DOM</title>
</head>
<body>
	<div id="d">hello dom</div>
	<script>
		function add(){
			var a = document.createElement("a");//创建元素节点
			var content = document.createTextNode("http://12306.com");//创建内容节点
			a.appendChild(content);//将内容节点追加到元素节点中

			var href = document.createAttribute("href");//创建属性节点
			href.nodeValue = "http://12306.com";//通过nodeValue属性设置值
			a.setAttributeNode(href);//将该属性设置到元素节点上

			var div1 = document.getElementById('d');//获取元素
			div1.appendChild(a);//追加
		}
	</script>
	<button onclick="add()">div中追加一个超链接</button>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_43293451/article/details/91424610