Blogger Information
Blog 11
fans 0
comment 0
visits 21145
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
根据class获取元素
玄夜的博客
Original
8215 people have browsed it
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<ul class="ul">
			<li class="red">列表项01</li>
			<li>列表项02</li>
			<li class="green">列表项03</li>
			<li>列表项04</li>
			<li class="yellow">列表项05</li>
		</ul>
	</body>
	
	<script>
		var red = document.getElementsByClassName('red');
//		console.log(red);
		red[0].style.background = 'red'
		
		//改方法不仅可以在document对象上调用,也可以在元素上调用,一般是在父元素上调用
		document.getElementsByClassName('ul').item(0)
				.getElementsByClassName('green').item(0)
				.style.background = 'green'
				
		//class支持多值
		var yellow = document.getElementsByClassName('yellow')[0];
		yellow.style.background = 'yellow';
		yellow.style.fontSize = '1.5rem'
	</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