下面是完整的代码:
我是想通过,鼠标移到tr上面,把背景样式改变,然后,鼠标单击的时候,也该表背景颜色,
现在这一步已经完成, 还有一个就是鼠标移到其他行,又需要恢复,onmouseover和 onmouseOut 的样式。 这个要怎么实现?请教一下...
<html>
<head>
<script>
//单击时,改变样式;
function onClickChangeStyle(obj){
//获取表格对象;
var tab = document.getElementById("tab");
//获取当前行选择下标;
var currentRowIndex = obj.rowIndex; //获取表格所有行数;
var tablRows = tab.rows.length;
//获取表格第一行,第一列的值;
//var firstCellValue = tab.rows[0].cells[0].innerHTML;
//获取表格的第一行,第一列的第一个元素的值;
//var firstChildValue = tab.rows[0].cells[0].firstChild.value;
//循环表格的所有行;并且选择的当前行,改变背景颜色;
for(var i = 0;i<tablRows;i=i+1){
if(currentRowIndex == i){
//为选中的当前,设置css样式;
tab.rows[i].style.cssText="background-color:00FFFF";
}else{
//把没有选中的行的背景样式设置为白色;
tab.rows[i].style.cssText="background-color:white";
tab.rows[i].onmouseover = function(){
this.className="displayStyle";
//tab.rows[i].className="displayStyle";
}
//绑定鼠标移开事件,并且设置样式为白色;
tab.rows[i].onmouseout = function()
{
this.cssText="background-color:white;";
//tab.rows[i].className="hideStyle";
}
}
}
}
</script> <style>
.displayStyle{
background-color:blue;
} .hideStyle{
background-color:white;
} .onClickStyle{
background-color:red;
}
</style>
<title>www.yuanshi88.com</title>
</head> <body>
<form name="myForm">
<table width="100%" id="tab" name="tab">
<tr id="tr1" onmouseOver=this.className="displayStyle" onmouseOut=this.className="hideStyle" onclick="onClickChangeStyle(this)">
<td><input type="checkbox" value="11"/></td>
<td>100</td>
<td>张三</td>
<td>15</td>
<td>湖南株洲</td>
</tr>
<tr id="tr2" onmouseOver=this.className="displayStyle" onmouseOut=this.className="hideStyle" onclick="onClickChangeStyle(this)">
<td><input type="checkbox" value="22"/></td>
<td>100</td>
<td>李四</td>
<td>15</td>
<td>湖南长沙</td>
</tr>
<tr id="tr3" onmouseOver=this.className="displayStyle" onmouseOut=this.className="hideStyle" onclick="onClickChangeStyle(this)">
<td><input type="checkbox" value="33"/></td>
<td>100</td>
<td>王五</td>
<td>15</td>
<td>湖南湘潭</td>
</tr>
</table>
</form>
</body>
</html>
我是想通过,鼠标移到tr上面,把背景样式改变,然后,鼠标单击的时候,也该表背景颜色,
现在这一步已经完成, 还有一个就是鼠标移到其他行,又需要恢复,onmouseover和 onmouseOut 的样式。 这个要怎么实现?请教一下...
<html>
<head>
<script>
//单击时,改变样式;
function onClickChangeStyle(obj){
//获取表格对象;
var tab = document.getElementById("tab");
//获取当前行选择下标;
var currentRowIndex = obj.rowIndex; //获取表格所有行数;
var tablRows = tab.rows.length;
//获取表格第一行,第一列的值;
//var firstCellValue = tab.rows[0].cells[0].innerHTML;
//获取表格的第一行,第一列的第一个元素的值;
//var firstChildValue = tab.rows[0].cells[0].firstChild.value;
//循环表格的所有行;并且选择的当前行,改变背景颜色;
for(var i = 0;i<tablRows;i=i+1){
if(currentRowIndex == i){
//为选中的当前,设置css样式;
tab.rows[i].style.cssText="background-color:00FFFF";
}else{
//把没有选中的行的背景样式设置为白色;
tab.rows[i].style.cssText="background-color:white";
tab.rows[i].onmouseover = function(){
this.className="displayStyle";
//tab.rows[i].className="displayStyle";
}
//绑定鼠标移开事件,并且设置样式为白色;
tab.rows[i].onmouseout = function()
{
this.cssText="background-color:white;";
//tab.rows[i].className="hideStyle";
}
}
}
}
</script> <style>
.displayStyle{
background-color:blue;
} .hideStyle{
background-color:white;
} .onClickStyle{
background-color:red;
}
</style>
<title>www.yuanshi88.com</title>
</head> <body>
<form name="myForm">
<table width="100%" id="tab" name="tab">
<tr id="tr1" onmouseOver=this.className="displayStyle" onmouseOut=this.className="hideStyle" onclick="onClickChangeStyle(this)">
<td><input type="checkbox" value="11"/></td>
<td>100</td>
<td>张三</td>
<td>15</td>
<td>湖南株洲</td>
</tr>
<tr id="tr2" onmouseOver=this.className="displayStyle" onmouseOut=this.className="hideStyle" onclick="onClickChangeStyle(this)">
<td><input type="checkbox" value="22"/></td>
<td>100</td>
<td>李四</td>
<td>15</td>
<td>湖南长沙</td>
</tr>
<tr id="tr3" onmouseOver=this.className="displayStyle" onmouseOut=this.className="hideStyle" onclick="onClickChangeStyle(this)">
<td><input type="checkbox" value="33"/></td>
<td>100</td>
<td>王五</td>
<td>15</td>
<td>湖南湘潭</td>
</tr>
</table>
</form>
</body>
</html>
解决方案 »
- 表格嵌套,Jquery取值问题
- JQuery指定区域的单击事件
- 关于弹出层的问题
- js设置select中value="paraValue"的Item为选中
- 这个怎么实现啊 在线等
- [求助高手] ASP弹出窗口问题..........
- CreateObject("WScript.Shell")失败
- SOS:如何在JavaScript中实现这个小功能? many thanx :)
- 使用javascript得到机器的情况比如机器名 机器的ip 和机器硬盘大小 内存大小 可以实现吗???
- 如何保护javascript代码?
- 不间断图片左右滚动代码火狐不兼容
- 谁能告诉我为什么删除第二行删除不了 我已经找到第二行了啊
<html>
<head>
<script>
window.onload=function(){
var nodes=document.getElementsByTagName("tr");
for(var i=0;i<nodes.length;i++){
nodes[i].onmouseover=function(){this.style.backgroundColor="blue"};
nodes[i].onmouseout=function(){this.style.backgroundColor="white"};
nodes[i].onclick=function(){this.style.backgroundColor="red"};
}
}
</script>
<title>www.yuanshi88.com</title>
</head> <body>
<form name="myForm">
<table width="100%" id="tab" name="tab">
<tr id="tr1">
<td><input type="checkbox" value="11"/></td>
<td>100</td>
<td>张三</td>
<td>15</td>
<td>湖南株洲</td>
</tr>
<tr id="tr2">
<td><input type="checkbox" value="22"/></td>
<td>100</td>
<td>李四</td>
<td>15</td>
<td>湖南长沙</td>
</tr>
<tr id="tr3" >
<td><input type="checkbox" value="33"/></td>
<td>100</td>
<td>王五</td>
<td>15</td>
<td>湖南湘潭</td>
</tr>
</table>
</form>
</body>
</html>
这样如何?
.displayStyle {
background-color: blue;
}
.hideStyle {
background-color: white;
}
.onClickStyle {
background-color: red;
}
</style>
<script>
$(document).ready(function(){
$("tr").mouseover(function(){
$(this).attr('class','displayStyle');
});
$("tr").mouseout(function(){
$(this).attr('class','hideStyle');
});
$("tr").click(function(){
$(this).attr('class','onClickStyle');
});
});
</script>