Javaweb 鼠標移入移出表格顏色變化的實現(xiàn)
最近在學習JavaWeb時,有用到鼠標移動事件,所以今天在這里記錄一個相關的案例,同時也是對相關知識的一個鞏固,效果為在鼠標移動到表格對應行列時,該行列的背景顏色發(fā)生變化。
效果如下:

其中用到是onmouseover和onmouseou事件t,同時還有一個作用相似的事件叫做onmousemove,所以在這里先對這三種鼠標事件做一個簡單的對比:
- 在時間上:如果兩個事件同時存在,先是onmousemove事件觸發(fā)后,才會觸發(fā)onmouseover事件。
- 在按鈕上:onmousemove和onmouseover都不區(qū)分鼠標按鈕
- 在動作上:onmouseover是在鼠標剛移入?yún)^(qū)域的時候觸發(fā),onmousemove是除了鼠標移入?yún)^(qū)域時觸發(fā)外,鼠標在區(qū)域內(nèi)移動同樣也會觸發(fā)事件。
- 兩者區(qū)別:當鼠標移過當前對象區(qū)域時就產(chǎn)生了onmouseover事件,所以onmouseover事件有個移入移出的過程,當鼠標在當前對象區(qū)域上移動時就產(chǎn)生了onmousemove事件,只要是在對象上移動而且沒有移出對象的,那么就是onmousemove事件。
onmouseout事件則是在鼠標移出對象區(qū)域時觸發(fā)。
搞懂這三者之間的關系,在進行鼠標經(jīng)過事件的處理時只需使用對應的事件觸發(fā)即可:
接下來是對上述事件和效果的代碼:
Jsp部分代碼:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" > <title>表格顏色變化</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" > --> <script type="text/javascript" src="index.js"></script> </head> <body> <table width = "200" border = "1" align = "center" cellpadding="1" cellspacing="5"> <tr id = "t0"><th>學校</th><th>專業(yè)</th><th>人數(shù)</th></tr> <tr id = "t1"><th>濟大</th><th>軟件</th><th>2000</th></tr> <tr id = "t2"><th>北大</th><th>機械</th><th>3000</th></tr> <tr id = "t3"><th>浙大</th><th>生物</th><th>4000</th></tr> </table> </body> </html>
Js部分代碼:
onload = function() {
var t0 = document.getElementById("t0");
var t1 = document.getElementById("t1");
var t2 = document.getElementById("t2");
var t3 = document.getElementById("t3");
t0.onmouseover = function () {
t0.style.backgroundColor = "green";
}
t0.onmouseout = function () {
t0.style.backgroundColor = "white";
}
t1.onmouseover = function () {
t1.style.backgroundColor = "red";
}
t1.onmouseout = function () {
t1.style.backgroundColor = "white";
}
t2.onmouseover = function () {
t2.style.backgroundColor = "red";
}
t2.onmouseout = function () {
t2.style.backgroundColor = "white";
}
t3.onmouseover = function () {
t3.style.backgroundColor = "red";
}
t3.onmouseout = function () {
t3.style.backgroundColor = "white";
}
}
到此這篇關于Javaweb 鼠標移入移出表格顏色變化的實現(xiàn)的文章就介紹到這了,更多相關Javaweb 鼠標移入移出表格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MyBatis中批量插入和批量更新的實現(xiàn)方法詳解
這篇文章主要介紹了MyBatis中批量插入和批量更新的實現(xiàn)方法,在日常開發(fā)中有時候需要從A數(shù)據(jù)庫提取大量數(shù)據(jù)同步到B系統(tǒng),這種情況自然是需要批量操作才行,感興趣想要詳細了解可以參考下文2023-05-05
springboot處理url中帶斜杠/\字符的參數(shù)報400問題
這篇文章主要介紹了springboot處理url中帶斜杠/\字符的參數(shù)報400問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
Eclipse+Java+Swing+Mysql實現(xiàn)電影購票系統(tǒng)(詳細代碼)
這篇文章主要介紹了Eclipse+Java+Swing+Mysql實現(xiàn)電影購票系統(tǒng)并附詳細的代碼詳解,需要的小伙伴可以參考一下2022-01-01
Spring Boot打開URL出現(xiàn)signin問題的解決
這篇文章主要介紹了Spring Boot打開URL出現(xiàn)signin問題的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
java對象類型轉(zhuǎn)換和多態(tài)性(實例講解)
下面小編就為大家?guī)硪黄猨ava對象類型轉(zhuǎn)換和多態(tài)性(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10

