鼠標(biāo)改背景下代碼onmouseover和onmouseout如何使用最上方的兩個(gè)函數(shù) 改來(lái)改去都不行
<!DOCTYPE html>
<html>
?<head>
? <title> new document </title>??
? <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>? ?
? <script type="text/javascript">?
? ? ? ? function on(node){
? ? ? ? ? ? node.style.backgroundColor = "#f2f2f2";
? ? ? ? }
? ? ? ? function out(node){
? ? ? ? ? ? node.style.backgroundColor = "#fff";
? ? ? ? }
? ? ? window.onload = function(){
? ? ? ? ? ? ? ? ??
? ? ?// 鼠標(biāo)移動(dòng)改變背景,可以通過給每行綁定鼠標(biāo)移上事件和鼠標(biāo)移除事件來(lái)改變所在行背景色。
? ? ? ? var row = document.getElementsByTagName("tr");
? ? ? ? for (var i = 0; i < row.length; i++){
? ? ? ? ? ? row[i].onmouseover = function (){
? ? ? ? ? ? ? ? this.style.backgroundColor = "#f2f2f2";
? ? ? ? ? ? }
? ? ? ? ? ? row[i].onmouseout = function (){
? ? ? ? ? ? ? ? this.style.backgroundColor = "#fff";
? ? ? ? ? ? }
? ? ? ? }
? ??
? ? ?
? ?
? ?}
? ? ?// 創(chuàng)建刪除函數(shù)
? ? ?function dele(node){
? ? ? ? var tr = node.parentNode.parentNode;
? ? ? ? tr.parentNode.removeChild(tr);
? ? ?}??
? ? ?
? ? ?
? ? ?
? ? ? // 編寫一個(gè)函數(shù),供添加按鈕調(diào)用,動(dòng)態(tài)在表格的最后一行添加子節(jié)點(diǎn);
? ? ?function add(){
? ? ? ? var table = document.getElementsByTagName("tbody")[0];
? ? ? ? var r = document.createElement("tr");
? ? ? ? r.setAttribute("onmouseover","on(this)");
? ? ? ? r.setAttribute("onmouseout","out(this)");
? ? ? ? table.appendChild(r);
? ? ? ? for (var i = 3; i > 0; i--){
? ? ? ? ? ? var h = document.createElement("td");
? ? ? ? ? ? if(i == 3){
? ? ? ? ? ? ? ? var num = document.getElementsByTagName("tr").length-1;
? ? ? ? ? ? ? ? if (num < 10){
? ? ? ? ? ? ? ? ? ? h.innerHTML = "xh00"+num;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if(num < 100){
? ? ? ? ? ? ? ? ? ? h.innerHTML = "xh0"+num;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else h.innerHTML = "xh"+num;
? ? ? ? ? ? }
? ? ? ? ? ? if (i == 2){
? ? ? ? ? ? ? ? h.innerHTML = prompt("請(qǐng)輸入姓名","如:張三");
? ? ? ? ? ? }
? ? ? ? ? ? if (i == 1){
? ? ? ? ? ? ? ? var a = document.createElement("a");
? ? ? ? ? ? ? ? a.href = "javascript:";
? ? ? ? ? ? ? ? a.innerHTML = "刪除";
? ? ? ? ? ? ? ? a.setAttribute("onclick","dele(this)");
? ? ? ? ? ? ? ? h.appendChild(a);
? ? ? ? ? ? }
? ? ? ? ? ? r.appendChild(h);
? ? ? ? }
? ? ? ??
? ? }
? ? ? ??
? ? ?
? </script>?
?</head>?
?<body>?
? ? ?<table border="1" width="50%" id="table">
? ? ?<tr>
? ? <th>學(xué)號(hào)</th>
? ? <th>姓名</th>
? ? <th>操作</th>
? ? ?</tr>??
? ? ?<tr>
? ? <td>xh001</td>
? ? <td>王小明</td>
? ? <td><a href="javascript:" onclick=dele(this)>刪除</a></td>? ?<!--在刪除按鈕上添加點(diǎn)擊事件? -->
? ? ?</tr>
? ? ?<tr>
? ? <td>xh002</td>
? ? <td>劉小芳</td>
? ? <td><a href="javascript:"onclick=dele(this) >刪除</a></td>? ?<!--在刪除按鈕上添加點(diǎn)擊事件? -->
? ? ?</tr>??
? ? ?</table>
? ? ?<input type="button" value="添加一行" onclick=add() />? ?<!--在添加按鈕上添加點(diǎn)擊事件? -->
?</body>
</html>
2020-05-26
這樣也可以:
row[i].onmouseover=function(){on(this)};
2020-05-26
設(shè)置鼠標(biāo)事件的時(shí)候可以改成這樣:
row[i].setAttribute("onmouseover",'javascript:on(this)');
row[i].setAttribute("onmouseout",'javascript:out(this)');