87,904
社区成员
发帖
与我相关
我的任务
分享
var xmlNodes;
//判断浏览器类型
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
}
else // for older IE 5/6
{
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", XMLPath, false);
xhttp.send("");
xmlDoc = xhttp.responseXML;
//nodeType clientLeft offsetParent parentElement offsetWidth localName ownerDocument nodeValue
//lastElementChild offsetLeft previousSibling tagName namespaceURI childElementCount scrollLeft
//lastChild clientHeight textContent nextSibling scrollWidth offsetHeight clientWidth nodeName style
//scrollTop baseURI offsetTop nextElementSibling firstChild attributes parentNode clientTop childNodes prefix
//previousElementSibling firstElementChild scrollHeight getAttribute setAttribute removeAttribute getAttributeNode
//getElementsByTagName getAttributeNS setAttributeNS removeAttributeNS getElementsByTagNameNS getAttributeNodeNS
//hasAttribute hasAttributeNS focus blur scrollIntoView scrollIntoViewIfNeeded scrollByLines scrollByPages
//getElementsByClassName querySelector querySelectorAll webkitMatchesSelector getClientRects getBoundingClientRect
//setAttributeNode removeAttributeNode setAttributeNodeNS contains insertBefore replaceChild removeChild appendChild
//hasChildNodes cloneNode normalize isSupported hasAttributes lookupPrefix isDefaultNamespace lookupNamespaceURI addEventListener
//removeEventListener isSameNode isEqualNode compareDocumentPosition dispatchEvent ELEMENT_NODE ATTRIBUTE_NODE TEXT_NODE
//CDATA_SECTION_NODE ENTITY_REFERENCE_NODE ENTITY_NODE PROCESSING_INSTRUCTION_NODE COMMENT_NODE DOCUMENT_NODE
//DOCUMENT_TYPE_NODE DOCUMENT_FRAGMENT_NODE NOTATION_NODE DOCUMENT_POSITION_DISCONNECTED DOCUMENT_POSITION_PRECEDING
//DOCUMENT_POSITION_FOLLOWING DOCUMENT_POSITION_CONTAINS DOCUMENT_POSITION_CONTAINED_BY DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
//此种方法谷歌下可以,但是火狐下就又不行了。
xmlNodes = xmlDoc.documentElement.childNodes
var obj = xmlNodes[1];
for (var i = 1; i <= xmlNodes.length; i++) {
//alert(xmlNodes[1].nextElementSibling.tagName);
var provinceNode = obj.getAttribute("name");
var pOption = document.createElement("option");
pOption.value = provinceNode;
pOption.text = provinceNode;
provinceDropDownList.add(pOption);
obj = obj.nextElementSibling;
}
try {
xmlDoc.load(XMLPath);
} catch (e) {alert(e); }
xmlDoc.async = false;
xmlDoc.onload = function () {
xmlNodes = xmlDoc.getElementsByTagName("province");
for (var i = 0; i < xmlNodes.length; i++) {
var provinceNode = xmlNodes[i].getAttribute("name");
var pOption = document.createElement("option");
pOption.innerHTML = provinceNode;
pOption.setAttribute("value", provinceNode);
provinceDropDownList.appendChild(pOption);
}
<html>
<body>
<script type="text/javascript">
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else // for older IE 5/6
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET","books.xml",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;
document.write(xmlDoc.documentElement.nodeName + " loaded");
</script>
</body>
</html>