简体   繁体   English

将UL添加到LI(不将UL添加到UL)

[英]Add a UL to a LI (not add a LI to a UL)

I am trying to add a UL inside of a LI. 我想在LI里面添加一个UL。 I have an HTML tree that looks like this: 我有一个看起来像这样的HTML树:

<li id="node0"><a href="#" onclick="Collapse(event)"><img src="file:///C:/drag-drop-folder-tree/images/dhtmlgoodies_minus.gif"></a>All My Windows
<ul style="display: block;" id="tree_ul_0">
<li id="node0" ondragenter="return dragEnter(event)" ondragover="allowDrop(event)" ondrop="drop(event)" ondragstart="drag(event)" draggable="True"><a href="#">9:00-9:30 moving up</a></li>
<li id="node1" ondragenter="return dragEnter(event)" ondragover="allowDrop(event)" ondrop="drop(event)" ondragstart="drag(event)" draggable="True"><a href="#">9:00-9:30 moving down</a>

I am trying to use Javascript to add a new UL that will be a child of the LI node0 at the top. 我正在尝试使用Javascript添加一个新的UL,它将成为顶部LI节点0的子节点。

Here is my code: 这是我的代码:

 var newul = document.createElement("ul");


 var ulist = document.getElementById("node0");
 var newItem = document.createElement("li");

 newItem.innerHTML = "<ul id='item1'><li id='Item0' ondragenter='return dragEnter(event)' ondragover='allowDrop(event)' ondrop='drop(event)' ondragstart='drag(event)' draggable='True'><a onclick='Collapse(event)' href='#'><img src='dhtmlgoodies_minus.gif'></a><text>hello</text></li></ul>"

 ulist.appendChild(newItem); 

When I execute this code, I end up with: 当我执行此代码时,我最终得到:

<li>
<ul id="item1">
<li id="Item0" ondragenter="return dragEnter(event)" ondragover="allowDrop(event)" ondrop="drop(event)" ondragstart="drag(event)" draggable="True"><a onclick="Collapse(event)" href="#"><img src="dhtmlgoodies_minus.gif"></a><text>hello</text>
</li>
</ul>
</li>

So it adds a new: 所以它增加了一个新的:

<LI>

which is a child of node0 这是node0的子节点

I would like for my item1 UL to be the child of node0. 我希望我的item1 UL成为node0的子节点。 Can you tell me how to add the UL without getting the extraneous LI? 你能告诉我如何在不获得无关的LI的情况下添加UL吗?

You overcomplicated it. 你过度复杂了。 You already got the node and you can just set its innerHTML: 你已经有了节点,你可以设置它的innerHTML:

var ulist = document.getElementById("node0");

ulist.innerHTML = "<ul id='item1'><li id='Item0' ondragenter='return dragEnter(event)' ondragover='allowDrop(event)' ondrop='drop(event)' ondragstart='drag(event)' draggable='True'><a onclick='Collapse(event)' href='#'><img src='dhtmlgoodies_minus.gif'></a><text>hello</text></li></ul>";

Alternatively, you can create an ul and an li in it and add that, but then you need to set add the proper element, the ul, to the node. 或者,您可以在其中创建ul和li并添加它,但是您需要设置向节点添加适当的元素ul。 That could easily become a lot of code. 这很容易成为很多代码。 The snippet below doesn't even set all events and it doesn't create the a element inside the node. 下面的代码段甚至没有设置所有事件,也没有在节点内创建a元素。

var ulist = document.getElementById("node0");

// Create the new ul.
var newul = document.createElement("ul");
newul.id = 'item1';

// Create the new li
var newItem = document.createElement("li");
newitem.id = 'Item0';
newitem.ondragenter = dragEnter;
// ... and so on
newitem.setAttribute('draggable', true);

// Append the li to the ul
newul.appendChild(newItem);
// And append the ul to the parent li.
ulist.appendChild(newul);

In your current situation you mixed both. 在你目前的情况下,你混合了两者。 You create a new ul , which is never used. 您创建了一个从未使用过的新ul Then you create a new li . 然后你创建一个新的li Then you set the innerHTML of that li to the entire HTML chunk you want to have, and add it to the parent. 然后将该li的innerHTML设置为您想要的整个HTML块,并将其添加到父级。 The li you get in your document is the newitem you created in your code. 您在文档中获得的li是您在代码中创建的newitem

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

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