課程
/前端開發(fā)
/JavaScript
/JavaScript進(jìn)階篇
有根據(jù)內(nèi)容獲取節(jié)點的方法么
2018-12-01
源自:JavaScript進(jìn)階篇 9-13
正在回答
<body>
??<ul?id="test">
????<li>JavaScript</li>
????<li>HTML</li>
??</ul>
??<script?type="text/javascript">
????//?var?otest?=?document.getElementById("test");
????//?//創(chuàng)建一個新的結(jié)點,為節(jié)點修改內(nèi)容,或者其它屬性,將該節(jié)點通過appendchild()添加給其它節(jié)點
????//?var?newnode?=?document.createElement("li");
????//?newnode.innerHTML?=?"PHP";
????//?otest.appendChild(newnode);
????//插入結(jié)點insertBefore(newnode,?node)向已有的子節(jié)點前插入一個新的子節(jié)點
????var?otest_2?=?document.getElementById("test");
????var?nodeList?=?otest_2.childNodes;
????console.log(nodeList);
????function?insertNode(innerhtml,?string)?{
??????var?newnode?=?document.createElement("li");
??????var?newtext?=?document.createTextNode(string);
??????newnode.appendChild(newtext);
??????for?(let?i?=?0;?i?<?nodeList.length;?i++)?{
????????if?(nodeList[i].innerHTML?==?innerhtml)?{
??????????//
??????????otest_2.insertBefore(newnode,?nodeList[i]);
????????}
??????}
????}
????insertNode("HTML","PHP");
????//?insertNode("PHP","你可以的");
??</script>
</body>
你是卿嘉俊?
應(yīng)該是讓練習(xí)這個:
<script?type="text/javascript">??var?otest?=?document.getElementById("test");????var?li?=?document.createElement('li');??li.innerHTML="PHP";??var?html?=?otest.childNodes[otest.childNodes.length-1];??otest.insertBefore(li,?html);</script>
根據(jù)內(nèi)容找到子節(jié)點:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標(biāo)題文檔</title>
</head>
<ul id="test"><li>JavaScript</li><li>HTML</li></ul>?
?
<script type="text/javascript">
? var otest = document.getElementById("test");??
? var newnode = document.createElement("li");
? newnode.innerHTML="php";
? var num = otest.childNodes.length;
? for(var i=0;i<num;i++){
? ? ? if(otest.childNodes[i].innerHTML=="HTML"){
? ? ? ? ?otest.insertBefore(newnode,otest.childNodes[i]);?
? ? ? }
? }
??
? document.write(otest.childNodes.length);
</script>?
</html>
好像是用:node.innerHTML,
這樣?otest.childNodes[i].innerHTML == "HTML"
沒有.
舉報
本課程從如何插入JS代碼開始,帶您進(jìn)入網(wǎng)頁動態(tài)交互世界
1 回答獲取節(jié)點的方式???
4 回答看看三種獲取節(jié)點的方法
1 回答看看三種獲取節(jié)點的方法?
3 回答三種獲取節(jié)點的方法怎么不彈出來
2 回答點擊“看看三種獲取節(jié)點的方法”沒有窗口出來
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2020-06-09
<body>
??<ul?id="test">
????<li>JavaScript</li>
????<li>HTML</li>
??</ul>
??<script?type="text/javascript">
????//?var?otest?=?document.getElementById("test");
????//?//創(chuàng)建一個新的結(jié)點,為節(jié)點修改內(nèi)容,或者其它屬性,將該節(jié)點通過appendchild()添加給其它節(jié)點
????//?var?newnode?=?document.createElement("li");
????//?newnode.innerHTML?=?"PHP";
????//?otest.appendChild(newnode);
????//插入結(jié)點insertBefore(newnode,?node)向已有的子節(jié)點前插入一個新的子節(jié)點
????var?otest_2?=?document.getElementById("test");
????var?nodeList?=?otest_2.childNodes;
????console.log(nodeList);
????function?insertNode(innerhtml,?string)?{
??????var?newnode?=?document.createElement("li");
??????var?newtext?=?document.createTextNode(string);
??????newnode.appendChild(newtext);
??????for?(let?i?=?0;?i?<?nodeList.length;?i++)?{
????????if?(nodeList[i].innerHTML?==?innerhtml)?{
??????????//
??????????otest_2.insertBefore(newnode,?nodeList[i]);
????????}
??????}
????}
????insertNode("HTML","PHP");
????//?insertNode("PHP","你可以的");
??</script>
</body>
2019-08-20
你是卿嘉俊?
2019-04-17
應(yīng)該是讓練習(xí)這個:
2019-03-19
根據(jù)內(nèi)容找到子節(jié)點:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標(biāo)題文檔</title>
</head>
<body>
<ul id="test"><li>JavaScript</li><li>HTML</li></ul>?
?
<script type="text/javascript">
? var otest = document.getElementById("test");??
? var newnode = document.createElement("li");
? newnode.innerHTML="php";
? var num = otest.childNodes.length;
? for(var i=0;i<num;i++){
? ? ? if(otest.childNodes[i].innerHTML=="HTML"){
? ? ? ? ?otest.insertBefore(newnode,otest.childNodes[i]);?
? ? ? }
? }
??
? document.write(otest.childNodes.length);
??
??
</script>?
</body>
</html>
2018-12-31
好像是用:node.innerHTML,
這樣?otest.childNodes[i].innerHTML == "HTML"
2018-12-09
沒有.