使用javascript实现表格高亮(onmouseover()鼠标被移动元素上事件onmouseout()鼠标从某元素移开事件)

使用javascript实现表格高亮(onmouseover()鼠标被移动元素上事件onmouseout()鼠标从某元素移开事件)
技术分析
  1. 确定事件(onmouseover()鼠标被移动元素上事件onmouseout()鼠标从某元素移开事件)
代码步骤
  1. 首先确定事件onmouseover()鼠标移动到元素上触发事件,onmouseout()鼠标从某元素移开触发事件
  2. 书写这两个函数,设置元素属性即可。

代码事件

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>使用javascript实现高亮</title>
		<script type="text/javascript">
			function movemousever(val){
				document.getElementById("tr"+val).style.backgroundColor="red";
			}
			function movemouseout(val){
				document.getElementById("tr"+val).style.backgroundColor="white";
			}
		</script>
	</head>
	<body>
		<table border="1px" width="500px" height="300px" align="center">
			<thead>
				<tr>
					<th>序号</th>
					<th>姓名</th>
					<th>年龄</th>
				</tr>
			</thead>
			<tbody>
				<tr id="tr1" onmouseover="movemousever(1)" onmouseout="movemouseout(1)">
					<td>1</td>
					<td>小明</td>
					<td>20</td>
				</tr>
				<tr id="tr2" onmouseover="movemousever(2)" onmouseout="movemouseout(2)">
					<td>2</td>
					<td>小李</td>
					<td>21</td>
				</tr>
				<tr id="tr3" onmouseover="movemousever(3)" onmouseout="movemouseout(3)">
					<td>3</td>
					<td>小孙</td>
					<td>33</td>
				</tr>
				<tr id="tr4" onmouseover="movemousever(4)" onmouseout="movemouseout(4)">
					<td>4</td>
					<td>小凤</td>
					<td>23</td>
				</tr>
				<tr id="tr5" onmouseover="movemousever(5)" onmouseout="movemouseout(5)">
					<td>5</td>
					<td>小肖</td>
					<td>26</td>
				</tr>
				<tr id="tr6" onmouseover="movemousever(6)" onmouseout="movemouseout(6)">
					<td>6</td>
					<td>小绿</td>
					<td>27</td>
				</tr>
				
			</tbody>
		</table>
	</body>
</html>

页面效果展示
当鼠标移动到表格上背景就会变红,移开就会消失。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值