[英]how to remove and create new ul li tag in html using javascript
誰能幫我在d3js中完成這項工作
我的html標簽如下
<div id="source1"><ul><li>Source</li></ul></div>
我想通過刪除DIV id source1中的所有UL LI標簽並在div id source1中創建新的UL LI標簽來用<li>some other text</li>
修改<li>Source</li>
我的代碼在這里
d3.selectAll("ul").remove()
d3.selectAll("li").remove()
var test=document.getElementbyId('source1');
var ul=document.createElement('ul');
document.body.appendChild(test);
test.appendChild(ul);
var li=document.createElement('li');
ul.appendChild(li);
li.innerHTML="some other text";
這是應該工作的代碼:
var test=document.getElementById('source1');
var li=test.children[0].children[0];
li.innerHTML="some other text";
除了重新創建元素,您只能更改文本。
如果您的moto只是編輯li標記中的文本,則可以使用“ innerHtml” javascript方法。 這是最好的方法,例如。
<div id="source1"><ul><li id="liId">Source</li></ul></div>
document.getElementById('liId').innerHTML="Some other text";
如果您要替換element(在您的情況下不是必需的),則可以使用它,
<div id="source1">
<ul><li id="liId">Source</li></ul>
</div>
<script>
var para = document.createElement("li");
var node = document.createTextNode("Some other Text");
para.appendChild(node);
var parent = document.getElementById("source1");
var child = document.getElementById("liId");
parent.replaceChild(para,child);
</script>
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.