js-DOM首尾结点兼容性问题

firstElementChild
firstChild

<!doctype html>
<html>
<head>
<meta charset="utf-8">
	<script>
	
	
		window.onload=function(){
			var x = document.getElementById('ul1')
		if(x.firstElementChild){
			x.firstElementChild.style.background='red';
}else{
	x.firstChild.style.background='red';
}
			
		}	
	
	</script>
	<title>无标题文档</title>

	</head>
	
	
	<div id="ul1">
	
		<li>111</li>
		<li>222</li>
		<li>333</li>
	
	
	</div>
<body>
</body>
</html>

在这里插入图片描述

发布了62 篇原创文章 · 获赞 102 · 访问量 3159

猜你喜欢

转载自blog.csdn.net/weixin_44763595/article/details/104802612