Js获取table当前tr行的值的代码
作者:bea
下面的代码主要是学习控制表格里面的一些操作,代码都是直接写在表格里面的, 大家可以改成事件绑定的。 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>Js获取 table当前行的值</title> <script language=javascript> var selecte
下面的代码主要是学习控制表格里面的一些操作,代码都是直接写在表格里面的, 大家可以改成事件绑定的。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Js获取 table当前行的值</title>
<script language=javascript>
var selectedTr=null;
function c1(obj){
obj.style.backgroundColor='blue'; //把点到的那一行变希望的颜色;
if(selectedTr!=null) selectedTr.style.removeAttribute("backgroundColor");
if(selectedTr==obj) selectedTr=null;//加上此句,以控制点击变白,再点击反灰
else selectedTr=obj;
}
/*得到选中行的第一列的值*/
function check(){
if(selectedTr!=null){
var str=selectedTr.cells[0].childNodes[0].value;
document.getElementById("lab").innerHTML=str;
}
else{
alert("请选择一行");
}
}
/*删除选中行*/
function del(){
if(selectedTr!=null){
if(confirm("确定要删除吗?")){
alert(selectedTr.cells[0].childNodes[0].value);
var tbody=selectedTr.parentNode;
tbody.removeChild(selectedTr);
}
}
else{
alert("请选择一行");
}
}
</script>
</head>
<body>
单击选中Tr,高亮显示,再单击取消选选中。<input type=button value="选中的是哪一行?" onclick="check()"> <input
type=button value="删除选中行" onclick="del()"> <input type=button value="增加一行" onclick="add()">
<table width="100%" border="1" cellspacing="0" cellpadding="0" id="tab">
<tr onclick="c1(this);" bgcolor="#cccccc">
<td ><input type="text" value="11"> </td> <td><input type="text" value="12"></td>
</tr>
<tr onclick="c1(this);" bgcolor="#e0e0e0">
<td ><input type="text" value="21"> </td> <td><input type="text" value="22"></td>
</tr>
<tr onclick="c1(this);" bgcolor="#cccccc">
<td ><input type="text" value="31"> </td> <td><input type="text" value="32"></td>
</tr>
<tr onclick="c1(this);" bgcolor="#e0e0e0">
<td ><input type="text" value="41"> </td> <td><input type="text" value="42"></td>
</tr>
<tr onclick="c1(this);" bgcolor="#cccccc">
<td ><input type="text" value="51"> </td> <td ><input type="text" value="52"> </td>
</tr>
</table>
<label id="lab"></label>
</body>
</html>
[Ctrl+A 全选 注:
如需引入外部Js需刷新才能执行]
有用 | 无用
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Js获取 table当前行的值</title>
<script language=javascript>
var selectedTr=null;
function c1(obj){
obj.style.backgroundColor='blue'; //把点到的那一行变希望的颜色;
if(selectedTr!=null) selectedTr.style.removeAttribute("backgroundColor");
if(selectedTr==obj) selectedTr=null;//加上此句,以控制点击变白,再点击反灰
else selectedTr=obj;
}
/*得到选中行的第一列的值*/
function check(){
if(selectedTr!=null){
var str=selectedTr.cells[0].childNodes[0].value;
document.getElementById("lab").innerHTML=str;
}
else{
alert("请选择一行");
}
}
/*删除选中行*/
function del(){
if(selectedTr!=null){
if(confirm("确定要删除吗?")){
alert(selectedTr.cells[0].childNodes[0].value);
var tbody=selectedTr.parentNode;
tbody.removeChild(selectedTr);
}
}
else{
alert("请选择一行");
}
}
</script>
</head>
<body>
单击选中Tr,高亮显示,再单击取消选选中。<input type=button value="选中的是哪一行?" onclick="check()"> <input
type=button value="删除选中行" onclick="del()"> <input type=button value="增加一行" onclick="add()">
<table width="100%" border="1" cellspacing="0" cellpadding="0" id="tab">
<tr onclick="c1(this);" bgcolor="#cccccc">
<td ><input type="text" value="11"> </td> <td><input type="text" value="12"></td>
</tr>
<tr onclick="c1(this);" bgcolor="#e0e0e0">
<td ><input type="text" value="21"> </td> <td><input type="text" value="22"></td>
</tr>
<tr onclick="c1(this);" bgcolor="#cccccc">
<td ><input type="text" value="31"> </td> <td><input type="text" value="32"></td>
</tr>
<tr onclick="c1(this);" bgcolor="#e0e0e0">
<td ><input type="text" value="41"> </td> <td><input type="text" value="42"></td>
</tr>
<tr onclick="c1(this);" bgcolor="#cccccc">
<td ><input type="text" value="51"> </td> <td ><input type="text" value="52"> </td>
</tr>
</table>
<label id="lab"></label>
</body>
</html>
[Ctrl+A 全选 注:
如需引入外部Js需刷新才能执行]
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- JS+CSS实现的一种交互体验 表单页面
- javascript 仿开心网好友印象功能(点击文字弹出印象框)
- JavaScript 未知高度元素垂直居中实现代码
- 纯CSS实现的当鼠标移上图片添加阴影效果代码
- Div+Js实现的带阴影菜单 微软以前网站曾用过
- Js倒计时代码,当鼠标离开页面与回到页面会自动停止与开始
- javascript强制弹出新窗口实现代码
- javaScript 删除确认实现方法小结
- jquery 表单取值常用代码
- JavaScript是否可实现多线程 深入理解JavaScript定时机制
- JavaScript 图片预览效果 推荐
- javascript 年月日联动实现核心代码
- 兼容firefox的文本框只能输入两位小数的数字的代码
- js Firefox 加入收藏夹功能代码 兼容Firefox 和 IE
- Javascript和Ajax中文乱码吐血版解决方案
- 利用jQuery的$.event.fix函数统一浏览器event事件处理
- jQuery 连续列表实现代码
- jQuery解决iframe高度自适应代码
- IE 上下滚动展示模仿Marquee机制