XML及HTML文档解析
发布时间:2020-12-16 05:17:38 所属栏目:百科 来源:网络整理
导读:!DOCTYPE htmlhtml xmlns="http://www.w3.org/1999/xhtml"headmeta http-equiv="Content-Type" content="text/html; charset=utf-8" /link href="css/base.css" rel="stylesheet" type="text/css"titleXML及HTML文档解析/title/headbodydiv上一个/divdiv id=
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="css/base.css" rel="stylesheet" type="text/css"> <title>XML及HTML文档解析</title> </head> <body> <div>上一个</div> <div id="id">文本</div> <div>下一个</div> </body> </html> <script> var txt=document.getElementById("id").childNodes[0]; document.getElementById("id").childNodes[0].nodeValue="文本修改了"; text = txt.parentNode.nodeName; document.write("父节点名称:" + text+"<br/>"); text = txt.parentNode.parentNode.childNodes; child_Length = text.length; for (var i = 0; i< child_Length; i++) { document.write("Node " +(i + 1) + " : " + text[i].nodeName+"<br/>"); } text = txt.parentNode.parentNode.firstChild.nodeName; document.write("第一个节点名称:" + text+"<br/>"); text = txt.parentNode.parentNode.lastChild.nodeName; document.write("最后一个节点名称:" + text+"<br/>"); text = txt.parentNode.previousSibling.nodeName; document.write("上一个节点名称:" + text+"<br/>"); text = txt.parentNode.nextSibling.nodeName; document.write("下一个节点名称:" + text+"<br/>"); text = txt.parentNode.getAttribute("id"); document.write("ID=" + text+"<br/>"); text = txt.parentNode.getAttributeNode("id"); document.write("ID=" + text.nodeValue+"<br/>"); // 添加或修改属性值 txt.parentNode.setAttribute("data-src","http://www.jask.cn/img.jpg"); //修改属性值 txt.parentNode.getAttributeNode("data-src").nodeValue = "http://www.jask.cn/img.jpg"; txt.parentNode.removeAttribute("data-src"); // 删除当前节点 //txt.parentNode.parentNode.removeChild(txt.parentNode); //txt.parentNode.removeAttributeNode(txt.parentNode.getAttributeNode("data-src")); var div = document.createElement("div"); var txtNode = document.createTextNode("添加文本节点"); div.appendChild(txtNode); var comment = document.createComment("注释"); txt.parentNode.appendChild(comment); // id中添加一个div及内容 txt.parentNode.appendChild(div); var attr = document.createAttribute("cd"); attr.nodeValue="song"; txt.parentNode.setAttributeNode(attr); txt.insertData(0,"新加的"); var div1 = document.createElement("div"); var txtNode1 = document.createTextNode("添加文本节点1"); div1.appendChild(txtNode1); txt.parentNode.parentNode.insertBefore(div1,txt.parentNode.nextSibling); //cloneNode() 方法有一个参数(true 或 false)。该参数指示被复制的节点是否包括原节点的所有属性和子节点。 txt.parentNode.parentNode.insertBefore(div1.cloneNode(true),txt.parentNode) //txt.nextSibling.childNodes[0].replaceData(0,8,"hello"); //txt = document.getElementsByTagName("title").childNodes[0].ownerDocument; //alert(text); </script> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |