简体   繁体   English

如何使用javascript在html中删除和创建新的ul li标签

[英]how to remove and create new ul li tag in html using javascript

Can anyone help me to get this work in d3js 谁能帮我在d3js中完成这项工作

My html tag are as below 我的html标签如下

<div id="source1"><ul><li>Source</li></ul></div>

i want to modify the <li>Source</li> with <li>some other text</li> by removing all the UL LI tags in DIV id source1 and creating new UL LI tags in div id source1 我想通过删除DIV id source1中的所有UL LI标签并在div id source1中创建新的UL LI标签来用<li>some other text</li>修改<li>Source</li>

My code is here 我的代码在这里

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";

here is code that should work : 这是应该工作的代码:

var test=document.getElementById('source1');
var li=test.children[0].children[0];
li.innerHTML="some other text";

Instead of recreating element you can change text only. 除了重新创建元素,您只能更改文本。

If your moto is to just edit the text in the li tag, you can go for "innerHtml" javascript method. 如果您的moto只是编辑li标记中的文本,则可以使用“ innerHtml” javascript方法。 This is the best method Eg. 这是最好的方法,例如。

<div id="source1"><ul><li id="liId">Source</li></ul></div>
 document.getElementById('liId').innerHTML="Some other text";

If you want to replace element(in your case, which is not required) you can use this, 如果您要替换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>

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM