>  기사  >  웹 프론트엔드  >  JavaScript+CSS+DIV를 사용하여 테이블 색상을 변경하는 방법

JavaScript+CSS+DIV를 사용하여 테이블 색상을 변경하는 방법

一个新手
一个新手원래의
2017-09-22 10:36:231646검색

테이블을 설정하세요. 마우스가 특정 행으로 이동하면 색상이 변경됩니다. 마우스가 떠나면 색상이 원래 색상으로 변경됩니다. 각 줄에는 두 개의 마우스 이벤트가 필요합니다:

1. 마우스 재정의: onMouseOver

2. 마우스 아웃: onMouseOut

동시에 다음 두 이벤트는 이벤트 함수를 호출해야 합니다. 1. 구현 행 색상 설정:

resetColor(row)

2. 행 색상 변경 구현:

changeColor(row)

구현 코드:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
  <head>
    <title>变色表格示例</title>
	<script type="text/javascript">
	function changeColor(row){
	 document.getElementById(row).style.backgroundColor=&#39;#9999FF&#39;;
	}
	function resetColor(row){
	document.getElementById(row).style.backgroundColor=&#39;&#39;;
	}
	</script>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
  <table width="200" border="1" cellspacing="1" cellpadding="1" align="center">
  <tr><th>学校</th><th>专业</th><th>人数</th></tr>
  <tr align="center" id="row1" onMouseOver="changeColor(&#39;row1&#39;)"
   onMouseOut="resetColor(&#39;row1&#39;)">
   <th>北大</th><th>法律</th><th>2000</th>
   </tr>
   <tr align="center" id="row2" onMouseOver="changeColor(&#39;row2&#39;)"
    onMouseOut="resetColor(&#39;row2&#39;)">
    <th>清华</th><th>计算机</th><th>5000</th>
   </tr>
   <tr align="center" id="row3" onMouseOver="changeColor(&#39;row3&#39;)"
    onMouseOut="resetColor(&#39;row3&#39;)">
    <th>人大</th><th>经济</th><th>6000</th>
    </tr>
  </table>
  </body>
</html>

위 내용은 JavaScript+CSS+DIV를 사용하여 테이블 색상을 변경하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.