Blogger Information
Blog 11
fans 0
comment 0
visits 21115
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
根据css获取元素
玄夜的博客
Original
1356 people have browsed it
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<ul id="ul">
			<li class="red">列表项01</li>
			<li>列表项02</li>
			<li class="green">列表项03</li>
			<li class="green">列表项04</li>
			<li class="yellow">列表项05</li>
		</ul>
	</body>
	
	<script>
		var lists = document.querySelectorAll('li');   //返回所有选择器匹配的元素,是一个数组
//		console.log(lists)
		lists[0].style.background = 'pink'
		lists.item(1).style.background = 'yellow'
		
		//querySelector(All)方法也可以在元素上调用
		var ul = document.querySelector('#ul')   //querySelector  返回匹配的以第一个元素
//		console.log(ul)

		var li = ul.querySelectorAll('.green')
//		console.log(li)
		for(var i=0;i<li.length;i++){
			li[i].style.background = 'green'
		}
		
		
	</script>
</html>


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post