模板文字不是内插变量 [英] Template literals are not interpolating variables

查看:74
本文介绍了模板文字不是内插变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天刚刚注意到带有html标签的模板文字不起作用,或者我写错了吗?

Just noticed today that template literals with html tags don't work, or maybe I wrote it wrong?

我试图在模板文字中包含p标签(我在代码段中对此进行了注释),但这没有用.有人有什么想法吗?谢谢!

I tried to include p tags in the template literals (which I commented out in the snippet), but it didn't work. Does anyone have any ideas? Thanks!

var blueBtn = document.getElementById('btn');
var aniBox = document.getElementById('animal-info');

blueBtn.addEventListener('click', function() {
    var ourRequest = new XMLHttpRequest();
    ourRequest.open('GET', 'https://learnwebcode.github.io/json-example/animals-1.json');
    ourRequest.onload = function() {
        var ourData = JSON.parse(ourRequest.responseText);
        addHTML(ourData)
    };
    ourRequest.send();
});

function addHTML(data) {
    var content = '';
    for (let i of data) {
        console.log(i);
        content += '<p>' + i.name + ' is a ' + i.species + '.</p>';
 //content += '`<p>${i.name} is a ${i.species}.</p>`'; <--this one doesn't work
    }
    aniBox.insertAdjacentHTML('beforeend', content);
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>JSON and AJAX</title>
</head>
<body>
    <header>
        <h1>JSON and AJAX</h1>
        <button id="btn">Fetch Info for 3 New Animals</button>
    </header>

    <div id="animal-info"></div>

    <script src="js/main.js"></script>
</body>
</html>

推荐答案

需要将模板括在反引号中.您无需再次将模板用引号引起来.

Templates are needed to be enclosed in backticks. You don't need to enclose template in quotes again.

您需要更改此内容

'`<p>${i.name} is a ${i.species}.</p>`'

对此:

`<p>${i.name} is a ${i.species}.</p>`

前者只是一个普通的JavaScript字符串,而后者是模板文字语法,它允许对${ ... }中的部分进行插值.

The former is just a plain JavaScript string, but the latter is the template literal syntax and it allows the sections in ${ ... } to be interpolated.

请参见以下工作示例:

var blueBtn = document.getElementById('btn');
var aniBox = document.getElementById('animal-info');

blueBtn.addEventListener('click', function() {
  var ourRequest = new XMLHttpRequest();
  ourRequest.open('GET', 'https://learnwebcode.github.io/json-example/animals-1.json');
  ourRequest.onload = function() {
    var ourData = JSON.parse(ourRequest.responseText);
    addHTML(ourData)
  };
  ourRequest.send();
});

function addHTML(data) {
  var content = '';
  for (let i of data) {
    console.log(i);
    // content += '<p>' + i.name + ' is a ' + i.species + '.</p>';
    content += `<p>${i.name} is a ${i.species}.</p>`;
  }
  aniBox.insertAdjacentHTML('beforeend', content);
}

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>JSON and AJAX</title>
</head>

<body>
  <header>
    <h1>JSON and AJAX</h1>
    <button id="btn">Fetch Info for 3 New Animals</button>
  </header>

  <div id="animal-info"></div>

  <script src="js/main.js"></script>
</body>

</html>

详细了解模板文字文档.

这篇关于模板文字不是内插变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆