关于DOM元素
DOM獨立于具體的編程語言,通常通過JavaScript訪問DOM,不過并不嚴格要求這樣。可以使用任何腳本語言來訪問DOM,這要歸功于其一致的API。
DOM元素的屬性:
childNodes:返回當前元素所有的子元素的數組
firstChild:返回當前元素的第一個下級子元素
lastChild:返回當前元素的最后一個子元素
nextSibling:返回緊跟在當前元素后面的元素
nodeValue:指定表示元素值的讀/寫屬性
parentNode:返回元素的父節點
previousibling:返回緊鄰當前元素之前的元素
DOM元素的方法:
getElementById(id):獲取指定惟一ID屬性值文檔中的元素
getElementsByTagName(name):返回妝前元素中有指定標記名的子元素的數組
hasChildNodes():返回一個布爾值,指示元素是否有子元素
getAttribute(name):返回元素的屬性值,屬性由name指定
下面是一個例子:jsp代碼如下:
<head>
?<script language="javascript" type="text/javascript">
?var XMLrequest = false;
?var requsettype;
?try {
??request = new XMLHttpRequest();
?} catch (trymicrosoft) {
??try {
???XMLrequest = new ActiveXObject("Msxml2.XMLHTTP");
??} catch (othermicrosoft) {
???try {
????XMLrequest = new ActiveXObject("Microsoft.XMLHTTP");
???} catch (failed) {
????XMLrequest = false;
???}
??}
?}
?if (!XMLrequest){
??alert("Error initializing XMLHttpRequest!");
?}
?function startrequest(requestedList) {
??requsettype = requestedList;
??? var url = "response.xml";
??? // Open a connection to the server
??? XMLrequest.open("GET", url, true);
??? // Setup a function for the server to run when it's done
??? XMLrequest.onreadystatechange = updatePage;
??? // Send the request
??? XMLrequest.send(null);
??}
?function updatePage() {
??? if (XMLrequest.readyState == 4) {
???? if(requsettype=="north"){
????? listNorthStates();
????? }
???? else if(requsettype=="all"){
?????listAllStates();
????? }
????? }
??}
?function listNorthStates(){
???var xmlDoc = XMLrequest.responseXML;
???var northNode = xmlDoc.getElementsByTagName("north")[0];
???var out = "Northern States";
???var northStates = northNode.getElementsByTagName("state");
???outputList("Northern States",northStates);
??}
?function listAllStates(){
??var xmlDoc = XMLrequest.responseXML;
??var allStates = xmlDoc.getElementsByTagName("state");
??outputList("All States in Document",allStates);
?}
?function outputList(title,states){
???var out = title;
???var currentState = null;
???for(var i=0;i<states.length;i++){
?????currentState = states[i];
?????out = out+"\n-"+currentState.childNodes[0].nodeValue;
????}
???alert(out);
??}
</script>
?</head>
?<body>
??<button onClick="startrequest('all');">click me show all xml</button>
??<button onClick="startrequest('north');">click me show north xml</button>
?</body>
xml文件代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<states>
?<north>
??<state>a</state>
??<state>b</state>
??<state>c</state>
?</north>
?<south>
??<state>d</state>
??<state>e</state>
??<state>f</state>
?</south>
?<east>
??<state>g</state>
??<state>h</state>
??<state>i</state>
?</east>
?<west>
??<state>j</state>
??<state>k</state>
??<state>l</state>
?</west>
</states>
這是我對DOM的學習,望大家給出意見
轉載于:https://www.cnblogs.com/xingzhen/archive/2011/05/31/2065164.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
- 上一篇: Sqlserver别太信任SysComm
- 下一篇: Spring Boot 不使用默认的 p