简体   繁体   English

使用JavaScript将其添加到嵌套在另一个div中的div中

[英]Adding to a div that is nested inside another div using Javascript

Using a template off bootstrap but I have no idea how to add to their nested list. 在引导程序之外使用模板,但我不知道如何添加到其嵌套列表中。

I am able to add a class to the list item and append it to the unordered list, but that class has other components to it that I am unsure how to address. 我能够将一个类添加到列表项并将其附加到无序列表,但是该类还有其他组件,我不确定该如何解决。

<div id="contacts">
<ul>
 <li class="contact">
  <div class="wrap">
   <span class="contact-status online"></span>
    <img src="http://emilcarlsson.se/assets/louislitt.png" alt="">
     <div class="meta">
       <p class="name">Louis Litt</p>
       <p class="preview">You just got LITT up, Mike.</p>
    </div>
   </div>
  </li>
</ul>
</div>
  if (data.success) {
                const contents = `${data.chatroom}`
                // add the data to the list
                const li = document.createElement('li');
                li.classList.add('contact');
                li
                document.querySelector('#contacts').append(li);

I want it so that the list content is from my data that is sent back from the server. 我想要它,以便列表内容来自服务器发送回的我的数据。 How do I go about appending to this list changing the name to what ever I want and as well as the preview? 我如何追加到此列表,以将名称更改为我想要的名称以及预览? Is it contacts.contact.wrap.meta.name = "foo"? 它是contacts.contact.wrap.meta.name =“ foo”吗?

.querySelector can be attached to any element, not just the document . .querySelector可以附加到任何元素,而不仅仅是document So, just attach it to whatever element you want, eg: 因此,只需将其附加到所需的任何元素即可,例如:

document.querySelector('#parent-element-of-my-choice').querySelector('#contacts').append(li);

Couldn't you just do: 你不能只是做:

const name = document.querySelector('.name');
name.classList.add = "foo";

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

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