列表DOM

列表DOM

隔行变色

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		li{
			list-style: none;
		}
	</style>
</head>
<body>
	<input type="button" value="隔行变色" id="btn"/>
	<ul id="list">
		<li>1</li>
		<li>2</li>
		<li>3</li>
		<li>4</li>
		<li>5</li>
		<li>6</li>
		<li>7</li>
		<li>8</li>
		<li>9</li>
		<li>10</li>
	</ul>
	<script type="text/javascript">
		document.getElementById("btn").onclick = function () {
			var list = document.getElementById("list").getElementsByTagName("li");
			for (var i = 0; i < list.length; i++) {
				list[i].style.backgroundColor=i%2==0?"#ee6363":"#ff1493";
			}
		};
	</script>
</body>
</html>

列表项鼠标进入变色

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		li{
			list-style: none;
			cursor: pointer;
		}
	</style>
</head>
<body>
	<ul id="list">
		<li>1</li>
		<li>2</li>
		<li>3</li>
		<li>4</li>
		<li>5</li>
		<li>6</li>
		<li>7</li>
		<li>8</li>
		<li>9</li>
		<li>10</li>
	</ul>
	<script type="text/javascript">
			var list = document.getElementById("list").getElementsByTagName("li");
			for (var i = 0; i < list.length; i++) {
				list[i].onmouseover = function(){
					this.style.backgroundColor = "#7cfc00";
				}
				list[i].onmouseout = function(){
					this.style.backgroundColor = "";
				}
			}
		
	</script>
</body>
</html>
发布了116 篇原创文章 · 获赞 4 · 访问量 1774

猜你喜欢

转载自blog.csdn.net/qq_43618136/article/details/104251007
DOM