有下面这个table,tr是可以根据按钮上下移动的。
我想知道怎么用js取得每个td的内容?
如第一行里的内容是1,第二行的内容是2,我想得到字符串“1-2”;
移动后第一行的内容是2,第二行是1,我想得到字符串“2-1”
<table id="table1" bordercolor="#000000" width="200" border="1">
<tbody>
<tr>
<td width="25%">1</td>
<td width="25%"><a href="javascript:void(0)" onclick="moveUp(this)">上移</a></td>
<td width="25%"><a href="javascript:void(0)" onclick="moveDown(this)">下移</a></td>
</tr>
<tr>
<td>2</td>
<td><a href="javascript:void(0)" onclick="moveUp(this)">上移</a></td>
<td><a href="javascript:void(0)" onclick="moveDown(this)">下移</a></td>
</tr>
</tbody>
</table>
我想知道怎么用js取得每个td的内容?
如第一行里的内容是1,第二行的内容是2,我想得到字符串“1-2”;
移动后第一行的内容是2,第二行是1,我想得到字符串“2-1”
<table id="table1" bordercolor="#000000" width="200" border="1">
<tbody>
<tr>
<td width="25%">1</td>
<td width="25%"><a href="javascript:void(0)" onclick="moveUp(this)">上移</a></td>
<td width="25%"><a href="javascript:void(0)" onclick="moveDown(this)">下移</a></td>
</tr>
<tr>
<td>2</td>
<td><a href="javascript:void(0)" onclick="moveUp(this)">上移</a></td>
<td><a href="javascript:void(0)" onclick="moveDown(this)">下移</a></td>
</tr>
</tbody>
</table>
var obj=document.getElementById("table1");
var arr=[];
var rtn="";
for(var i=0;i<obj.rows.length;i++)
arr[i]=obj.rows[i].cells[0].innerHTML;
rtn=arr.join("-")
alert(rtn);
}
</SCRIPT>
<input type=button value=getTD onclick="getTD();">
<table id="table1" bordercolor="#000000" width="200" border="1">
<tbody>
<tr>
<td width="25%">1</td>
<td width="25%"><a href="javascript:void(0)" onclick="moveUp(this)">上移</a></td>
<td width="25%"><a href="javascript:void(0)" onclick="moveDown(this)">下移</a></td>
</tr>
<tr>
<td>2</td>
<td><a href="javascript:void(0)" onclick="moveUp(this)">上移</a></td>
<td><a href="javascript:void(0)" onclick="moveDown(this)">下移</a></td>
</tr>
</tbody>
</table>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function getRowNo(id) {
var table = document.getElementById(id);
var trs = table.firstChild.childNodes;
var retVal = "";
for (var index = 0; index < trs.length; index++) {
retVal = retVal + trs[index].getElementsByTagName('td')[0].firstChild.nodeValue + '-';
}
return retVal.substring(0, retVal.length - 1);
}</script>
</head>
<body>
<input type="button" onclick="alert(getRowNo('table1'))"></input>
<table id="table1" bordercolor="#000000" width="200" border="1">
<tbody>
<tr>
<td width="25%">1</td>
<td width="25%"><a href="javascript:void(0)" onclick="moveUp(this)">上移</a></td>
<td width="25%"><a href="javascript:void(0)" onclick="moveDown(this)">下移</a></td>
</tr>
<tr>
<td>2</td>
<td><a href="javascript:void(0)" onclick="moveUp(this)">上移</a></td>
<td><a href="javascript:void(0)" onclick="moveDown(this)">下移</a></td>
</tr>
</tbody>
</table>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js测试</title>
<style>
</style>
<script src="jquery-1.3.2.js"></script>
<script>
$(document).ready(function(){
$(".up").click(function(){
var this_text = $(this).parent().prev().text();//本次点击的文本值
var prev_text = $(this).parent().parent().prev().children(":eq(0)").text();
alert(this_text + "-" + prev_text)
});
$(".down").click(function(){
var this_text = $(this).parent().prev().prev().text();
var next_text = $(this).parent().parent().next().children(":eq(0)").text();
alert(this_text+"-"+next_text)
});
});
</script>
</head>
<body>
<table id="table1" bordercolor="#000000" width="200" border="1">
<tbody>
<tr>
<td width="25%">1</td>
<td width="25%"><a class="up" href="javascript:void(0)">上移</a></td>
<td width="25%"><a class="down" href="javascript:void(0)">下移</a></td>
</tr>
<tr>
<td>2</td>
<td><a class="up" href="javascript:void(0)">上移</a></td>
<td><a class="down" href="javascript:void(0)">下移</a></td>
</tr>
</tbody>
</table>
</body>
</html>