JavaScript XML 2 단계 연결 드 롭 다운 목록 구현
6649 단어 JavaScriptXML드 롭 다운 목록
<?xml version="1.0" encoding="GBK"?>
<select>
<province id="sx">
<city id="xa"> </city>
<city id="bj"> </city>
<city id="ak"> </city>
</province>
<province id="js">
<city id="nj"> </city>
<city id="xz"> </city>
</province>
<province id="sh">
</province>
</select>
2.HTML 페이지 생 성:select.html
<body>
</body>
<script>...
/**//**
* @description
* @author BluesLee
* @lastModif BluesLee
* @createDate 2007-10-13
* @modifDate 2007-10-15
* @version 1.0
*/
SelectLevel.prototype.xml;
SelectLevel.prototype.provinces;
SelectLevel.prototype.parentName="province";//
SelectLevel.prototype.childName="city";//
SelectLevel.prototype.keyName="id";//
/**//**
* SelectLevel
* @author BluesLee
* @lastModif BluesLee
* @createDate 2007-10-13
* @modifDate 2007-10-15
* @version 1.0
*/
function SelectLevel(parentName,childName,keyName)...{
if(parentName!=null && parentName!="")...{
this.parentName=parentName;
}
if(childName!=null && childName!="")...{
this.childName=childName;
}
if(keyName!=null && keyName!="")...{
this.keyName=keyName;
}
}
/**//**
* xml
* @author BluesLee
* @lastModif BluesLee
* @createDate 2007-10-13
* @modifDate 2007-10-15
* @version 1.0
*/
SelectLevel.prototype.readXML=function(url)...{
var selectXML;
// , 2 DOM
if(document.implementation && document.implementation.createDocument)...{
// Document
selectXML=document.implementation.createDocument("","",null);
//
selectXML.onload=function()...{
alert(" ");
}
selectXML.load(url);
}else...{//IE Document
selectXML=new ActiveXObject("Microsoft.XMLDOM");
// onload
selectXML.onreadystatechange=function()...{
if(selectXML.readyState==4)...{
alert(" ");
}
}
selectXML.load(url);
}
this.xml=selectXML;
}
/**//**
* xml
* @author BluesLee
* @lastModif BluesLee
* @createDate 2007-10-13
* @modifDate 2007-10-15
* @version 1.0
*/
SelectLevel.prototype.iteratorXML=function(node,level)...{
var n=node;
if(n==null)...{
n=this.xml.documentElement;
}
if(level==null)...{
level=0;
}
if(n.nodeType==3)...{
for(var i=0;i<level;i++)...{
document.write("-");
}
document.write(n.data.trim()+"<br>");
}else...{
for(var m=n.firstChild;m!=null;m=m.nextSibling)...{
this.iteratorXML(m,level+1);
}
}
}
/**//**
*
* @author BluesLee
* @lastModif BluesLee
* @createDate 2007-10-13
* @modifDate 2007-10-15
* @version 1.0
*/
SelectLevel.prototype.changeSelect=function()...{
var city=document.getElementById(this.childName);
var province=document.getElementById(this.parentName);
city.options.length=0;
if(province.value==null || province.value=="")...{
city.options.length=1;
city.options[0].text=" …";
return;
}
var citys=this.provinces[this[province.value]].getElementsByTagName(this.childName);
if(citys.length==0)...{
city.options.length=city.options.length+1;
city.options[city.options.length-1].value=province.value;
for(var i=0;i<province.options.length;i++)...{
if(province.options[i].selected)...{
city.options[city.options.length-1].text=province.options[i].text;
return;
}
}
return;
}
city.options.length=citys.length;
for(var i=0;i<citys.length;i++)...{
city.options[i].value=citys[i].getAttribute(this.keyName);
city.options[i].text=citys[i].firstChild.data.trim();
}
}
/**//**
*
* @author BluesLee
* @lastModif BluesLee
* @createDate 2007-10-13
* @modifDate 2007-10-15
* @version 1.0
*/
SelectLevel.prototype.init=function(parent,province,city)...{
this.provinces=this.xml.getElementsByTagName(this.parentName);
var selectProvince=document.createElement("select");
var selectCity=document.createElement("select");
var obj=this;
selectProvince.setAttribute("name",this.parentName);
selectProvince.setAttribute("id",this.parentName);
selectProvince.attachEvent("onchange",function()...{obj.changeSelect();});
selectProvince.options.length=this.provinces.length+1;
selectProvince.options[0].text=" …";
selectCity.setAttribute("name",this.childName);
selectCity.setAttribute("id",this.childName);
selectCity.options.length=1;
selectCity.options[0].text=" …";
for(var i=0;i<this.provinces.length;i++)...{
SelectLevel.prototype[this.provinces[i].getAttribute(this.keyName)]=i;
selectProvince.options[i+1].value=this.provinces[i].getAttribute(this.keyName);
selectProvince.options[i+1].text=this.provinces[i].firstChild.data.trim();
if(this.provinces[i].getAttribute(this.keyName)==province)...{
selectProvince.options[i+1].selected=true;
var citys=this.provinces[i].getElementsByTagName(this.childName);
selectCity.options.length=citys.length+1;
for(var j=0;j<citys.length;j++)...{
selectCity.options[j+1].value=citys[j].getAttribute(this.keyName);
selectCity.options[j+1].text=citys[j].firstChild.data.trim();
if(citys[j].getAttribute(this.keyName)==city)...{
selectCity.options[j+1].selected=true;
}
}
}
}
parent.appendChild(selectProvince);
parent.appendChild(selectCity);
}
String.prototype.trim=function()...{
return this.replace(/^s*/g,"").replace(/s*$/g,"");
}
//
var newXML=new SelectLevel();
newXML.readXML("select.xml");
//newXML.iteratorXML(null,-2);
newXML.init(document.body,"sx","bj");
</script>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
기초 정리 - 1문자 (String) 숫자 (Number) 불린 (Boolean) null undefined 심볼 (Symbol) 큰정수 (BigInt) 따옴표로 묶어 있어야 함 Not-A-Number - 숫자 데이터 / 숫자로 표...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.