如何在javascript中动态创建html元素

frebpwbc  于 2022-12-25  发布在  Java
关注(0)|答案(1)|浏览(110)

我有一个html页面,我想显示两个人之间的聊天内容,聊天框是这样的

<!-- right chat -->
<div class="chat__conversation-board__message-container reversed">
    <div class="chat__conversation-board__message__person">
        <div class="chat__conversation-board__message__person__avatar">
            <img src="./customer.jpg" alt="Dennis Mikle"/>
        </div>
        <span class="chat__conversation-board__message__person__nickname">Dennis Mikle</span>
    </div>
    <div class="chat__conversation-board__message__context">
        <div class="chat__conversation-board__message__bubble">
            <span>some text</span>
        </div>
    </div>
</div>

<!-- left chat -->
<div class="chat__conversation-board__message-container">
    <div class="chat__conversation-board__message__person">
        <div class="chat__conversation-board__message__person__avatar">
            <img src="./agent.jpg" alt="Monika Figi"/>
        </div>
        <span class="chat__conversation-board__message__person__nickname">Monika Figi</span>
    </div>
    <div class="chat__conversation-board__message__context">
        <div class="chat__conversation-board__message__bubble">
            <span>some text</span>
        </div>
    </div>
</div>

聊天内容的data.json是这样的

{
    "lyrics": [
        {
            "line": "line 1 start",
            "position": "r",
            "time": 4.5
        },
        {
            "line": "answer 1",
            "position": "l",
            "time": 9.03
        },
        {
            "line": "line 2 start",
            "position": "r",
            "time": 14.01
        }
    ]
}

我从服务器获取data.json并将其显示在index.html上,我希望在正文中创建position类型为(left=l or right=r)的元素

sz81bmfz

sz81bmfz1#

下面是我的示例代码,供您参考:

let data={
    "lyrics": [
        {
            "line": "line 1 start",
            "position": "r",
            "time": 4.5
        },
        {
            "line": "answer 1",
            "position": "l",
            "time": 9.03
        },
        {
            "line": "line 2 start",
            "position": "r",
            "time": 14.01
        }
    ]
}

function go(){
    let container;
   data.lyrics.forEach(lyric=>{
    if (lyric.position === 'r') {
        container=document.querySelector(".right .chat__conversation-board__message__bubble");
    } else {
        container=document.querySelector(".left .chat__conversation-board__message__bubble");
    }
     container.innerHTML+="<div>"+lyric.line+"</div>";
   })
   
}
<div class="chat__conversation-board__message-container reversed right">
  <div class="chat__conversation-board__message__person">
    <div class="chat__conversation-board__message__person__avatar">
      <img src="./customer.jpg" alt="Dennis Mikle" />
    </div>
    <span class="chat__conversation-board__message__person__nickname">Dennis Mikle</span>
  </div>
  <div class="chat__conversation-board__message__context">
    <div class="chat__conversation-board__message__bubble">
      <span>some text</span>
    </div>
  </div>
</div>

<!-- left chat -->
<div class="chat__conversation-board__message-container left">
  <div class="chat__conversation-board__message__person">
    <div class="chat__conversation-board__message__person__avatar">
      <img src="./agent.jpg" alt="Monika Figi" />
    </div>
    <span class="chat__conversation-board__message__person__nickname">Monika Figi</span>
  </div>
  <div class="chat__conversation-board__message__context">
    <div class="chat__conversation-board__message__bubble">
      <span>some text</span>
    </div>
  </div>
</div>

<button onclick="go()">
Go
</button>

我在容器的类名中添加了"right"和"left"。
因此,我可以根据"position"属性选择适当的容器来存储新内容。

相关问题