日期:2014-05-16  浏览次数:20476 次

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>