Javascript实现自动无限级关联下拉菜单
2007-09-07 13:43
337 查看
刚刚写完的,发上来大家共享一下
<html>
<head>
<title>级联下拉菜单</title>
<meta http-equiv = "Content-Type" counter = "text/html;charset = gb2312">
<script>...
var nMaxLength = 0;
function init(obj)
...{
var hasChild = false;
var nodes = document.getElementsByTagName('p');
var selectobj;
var pid = (obj==undefined) ? 0 : parseInt(obj.value);
var selectobj = document.createElement('select');
selectobj.setAttribute('name','select_' + nMaxLength);
selectobj.onchange = function()
...{
var sels = document.getElementsByTagName('select');
for(var j=sels.length-1;j>0;j--)
...{
if(sels[j]==this)
...{
break;
}
nMaxLength--;
document.getElementById('div1').removeChild(sels[j]);
}
init(this);
}
for(var i=0;i<nodes.length;i++)
...{
var thispid = parseInt(nodes[i].getAttribute('pid'));
var id = parseInt(nodes[i].getAttribute('id'));
var n = nodes[i].getAttribute('n');
if(thispid == pid)
...{
var opt = document.createElement('option');
opt.setAttribute('value',id);
opt.innerText = n;
selectobj.appendChild(opt);
hasChild = true;
}
}
if(hasChild)
...{
nMaxLength++;
document.getElementById('div1').appendChild(selectobj);
init(selectobj);
}
}
function getMaxLength()
...{
document.forms[0].maxLength.value = nMaxLength;
return true;
}
</script>
</head>
<body onload = "init();">
<form action ="" method="post" onsubmit="return getMaxLength();">
<div id="div1">
<p n='product' id='0' pid='-1' /><p n='art' id='1' pid='0' /><p n='english' id='2' pid='0' /><p n='game' id='3' pid='0' /><p n='math' id='4' pid='0' /><p n='natural' id='5' pid='0' /><p n='operate' id='6' pid='0' /><p n='poem' id='7' pid='0' /><p n='science' id='8' pid='0' /><p n='high' id='11' pid='1' /><p n='low' id='12' pid='1' /><p n='middle' id='13' pid='1' /><p n='abc' id='21' pid='2' /><p n='number' id='22' pid='2' /><p n='song' id='23' pid='2' /><p n='story' id='24' pid='2' /><p n='talk' id='25' pid='2' /><p n='listen' id='251' pid='25' /><p n='read' id='252' pid='25' /><p n='word' id='253' pid='25' /><p n='write' id='254' pid='25' /><p n='test' id='44' pid='1' /><p n='high' id='41' pid='4' />
</div>
<input type="hidden" name="maxLength" value="">
</form>
</body>
</html>
<html>
<head>
<title>级联下拉菜单</title>
<meta http-equiv = "Content-Type" counter = "text/html;charset = gb2312">
<script>...
var nMaxLength = 0;
function init(obj)
...{
var hasChild = false;
var nodes = document.getElementsByTagName('p');
var selectobj;
var pid = (obj==undefined) ? 0 : parseInt(obj.value);
var selectobj = document.createElement('select');
selectobj.setAttribute('name','select_' + nMaxLength);
selectobj.onchange = function()
...{
var sels = document.getElementsByTagName('select');
for(var j=sels.length-1;j>0;j--)
...{
if(sels[j]==this)
...{
break;
}
nMaxLength--;
document.getElementById('div1').removeChild(sels[j]);
}
init(this);
}
for(var i=0;i<nodes.length;i++)
...{
var thispid = parseInt(nodes[i].getAttribute('pid'));
var id = parseInt(nodes[i].getAttribute('id'));
var n = nodes[i].getAttribute('n');
if(thispid == pid)
...{
var opt = document.createElement('option');
opt.setAttribute('value',id);
opt.innerText = n;
selectobj.appendChild(opt);
hasChild = true;
}
}
if(hasChild)
...{
nMaxLength++;
document.getElementById('div1').appendChild(selectobj);
init(selectobj);
}
}
function getMaxLength()
...{
document.forms[0].maxLength.value = nMaxLength;
return true;
}
</script>
</head>
<body onload = "init();">
<form action ="" method="post" onsubmit="return getMaxLength();">
<div id="div1">
<p n='product' id='0' pid='-1' /><p n='art' id='1' pid='0' /><p n='english' id='2' pid='0' /><p n='game' id='3' pid='0' /><p n='math' id='4' pid='0' /><p n='natural' id='5' pid='0' /><p n='operate' id='6' pid='0' /><p n='poem' id='7' pid='0' /><p n='science' id='8' pid='0' /><p n='high' id='11' pid='1' /><p n='low' id='12' pid='1' /><p n='middle' id='13' pid='1' /><p n='abc' id='21' pid='2' /><p n='number' id='22' pid='2' /><p n='song' id='23' pid='2' /><p n='story' id='24' pid='2' /><p n='talk' id='25' pid='2' /><p n='listen' id='251' pid='25' /><p n='read' id='252' pid='25' /><p n='word' id='253' pid='25' /><p n='write' id='254' pid='25' /><p n='test' id='44' pid='1' /><p n='high' id='41' pid='4' />
</div>
<input type="hidden" name="maxLength" value="">
</form>
</body>
</html>
相关文章推荐
- 用javascript实现分级下拉菜单(可实现无限级)
- javascript(js),XML,DOM实现无限级下拉菜单,不会被任何标签或元素遮住
- select下拉菜单,无限加载切换菜单,实现自动加载子节点。
- Javascript实现关联数据(Linked Data)查询及注意细节
- 使用ASP与JAVASCRIPT配合实现多个复选框数据关联显示
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动
- JavaScript实现http地址自动检测并添加URL链接
- Javascript实现DIV滚动自动滚动到底部的代码
- javascript 实现页面加载完自动点击链接显示框架右侧信息页面
- Android平台上无线网卡自动扫描并关联AP的实现
- Javascript实现的自动验证函数
- 无限级下拉菜单--CSS+JS实现兼容性很好的无限级下拉菜单
- 基于javascript实现仿百度输入框自动匹配功能
- JavaScript实现的背景自动变色代码
- javascript实现页面刷新时自动清空表单并选中的方法
- 『原创』从屏幕右下角自动上升的窗口的Javascript实现
- Javascript实现的自动验证函数
- JavaScript实现url地址自动检测并添加URL链接示例代码
- 实现一个简单的Javascript级联下拉菜单