简体   繁体   English

如何在模板文字上使用html标签?

[英]How to html tags on template literals?

basically i want to print a list of the api but Im not sure if im using the template literal correctly 基本上我想打印一个api列表,但是我不确定IM是否正确使用模板文字

  <h6>XMLHttpRequest</h6>
  <ul class="testing"></ul>

html HTML

var xhr = new XMLHttpRequest()
xhr.onreadystatechange = function() {
  if(xhr.readyState == 4){
    if(xhr.status == 200){
        var text = JSON.parse(xhr.responseText)
        document.getElementsByClassName('testing')[0].textContent =
        `<li>User id: ${text.userId}</li>
         <li>Title: ${text.title}</li>
        `;
    }
  }
}

var url = "https://jsonplaceholder.typicode.com/posts/1";

xhr.open('GET', url, true)
xhr.send()

I have a feeling i did the template literal wrong. 我有种感觉,我的模板字面意思不对。 I need guidance lol 我需要指导大声笑

<li>User id: 1</li> <li>Title: sunt aut facere repellat provident occaecati excepturi optio reprehenderit</li>

this is my output but i want it in a list format minus the tags lol 这是我的输出,但我希望它以列表格式减去标签大声笑

Use innerHTML instead of textContent : 使用innerHTML代替textContent

 const xhr = new XMLHttpRequest() xhr.onreadystatechange = function() { if (xhr.readyState == 4) { if (xhr.status == 200) { const responseObj = JSON.parse(xhr.responseText) document.querySelector('.testing').innerHTML = `<li>User id: ${responseObj.userId}</li> <li>Title: ${responseObj.title}</li> `; } } } const url = "https://jsonplaceholder.typicode.com/posts/1"; xhr.open('GET', url) xhr.send() 
 <h6>XMLHttpRequest</h6> <ul class="testing"></ul> 

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

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