首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

js怎么查询节点

2012-05-30 
js如何查询节点Unions(p3,p2),也就是最后一个节点idp3怎么根据parent获取父级节点value, 最后拼装格式为

js如何查询节点
Unions(p3,p2),也就是最后一个节点id=p3 怎么根据parent获取父级节点value, 最后拼装格式为 a,b
父节点也可能很多个

HTML code
<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>


[解决办法]
HTML code
<!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>
[解决办法]
更正一下。。

HTML code
<!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>
这样试试

热点排行