[英]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.