js如何查询节点
Unions(p3,p2),也就是最后一个节点id=p3 怎么根据parent获取父级节点value, 最后拼装格式为 a,b
父节点也可能很多个
<td class="td_right"> <select id="p1" onchange="Unions(this.id,this.parent);" parent=""> <option selected="selected" value="a">a</option> </select> </td> <td class="td_right"> <select id="p2" onchange="Unions(this.id,this.parent);" parent="p1"> <option selected="selected" value="b">b</option> </select> </td> <td class="td_right"> <select id="p3" onchange="Unions(this.id,this.parent);" parent="p2"> <option selected="selected" value="c">c</option> </select> </td>
<!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>无标题文档</title><script type="text/javascript">function Unions(parentID) { var result = []; parentID = document.getElementById(parentID).getAttribute('parent'); while (parentID != '') { result.push(document.getElementById(parentID).value); parentID = document.getElementById(parentID).getAttribute('parent'); } alert(result.reverse());}</script></head><body><table> <tr> <td class="td_right"><select id="p1" onclick="Unions(this.id,this.parent);" parent=""> <option selected="selected" value="a">a</option> </select></td> <td class="td_right"><select id="p2" onclick="Unions(this.id,this.parent);" parent="p1"> <option selected="selected" value="b">b</option> </select></td> <td class="td_right"><select id="p3" onclick="Unions(this.id,this.parent);" parent="p2"> <option selected="selected" value="c">c</option> </select></td> </tr></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>无标题文档</title><script type="text/javascript">function Unions(parentID) { var result = []; if (parentID == '') return false; while (parentID != '') { result.push(document.getElementById(parentID).value); parentID = document.getElementById(parentID).getAttribute('parent'); } alert(result.reverse());}</script></head><body><table> <tr> <td class="td_right"><select id="p1" onclick="Unions(this.getAttribute('parent'));" parent=""> <option selected="selected" value="a">a</option> </select></td> <td class="td_right"><select id="p2" onclick="Unions(this.getAttribute('parent'));" parent="p1"> <option selected="selected" value="b">b</option> </select></td> <td class="td_right"><select id="p3" onclick="Unions(this.getAttribute('parent'));" parent="p2"> <option selected="selected" value="c">c</option> </select></td> </tr></table></body></html>
[解决办法]
<script type="text/javascript">
function init(){
var p=document.getElementById("test");
var c=p.getAttribute("value");
var par=p.parentNode;
if(par!=null){
c=par.getAttribute("value")+","+c;
while(par.parentNode){
par=par.parentNode;
if(par.getAttribute&&par.getAttribute("value")){
c=par.getAttribute("value")+","+c;
}
}
}
alert(c);
}
window.onload=init;
</script>
</head>
<body value="body">
<table value="table">
<tr value="tr">
<td value="td">
<p value="p" id="test">p</p>
</td>
</tr>
</table>
</body>
这样试试