let str = "aaaavvbb" //这是普通字符串
let str1 = `${"我是战士"}`
console.log(str1); //我是战士
let a = "我狄仁杰贼6"
let str1 = `${a}`
console.log(str1); //我狄仁杰贼6
//需求:假如a小于b,给box盒子添加active类
let box = document.querySelector(".box")
let a = 10;
let b = 20;
box.className = `${a<b?"active":""}`
//4:在模板字符串里面调用函数
function fn() {
console.log(1);
}
let str = `${fn()}` //调用函数fn,打印 1
//5:模板字符串使用map遍历数据
//需求:使用模板字符串遍历数组,给ul中的li添加数据
let ul = document.querySelector("ul");
let arr = [1, 3, 5, 7]
ul.innerHTML = arr.map((item, i) => {
return `
${item}
`
}).join("")//join("")主要是为了去掉,
let ul = document.querySelector("ul");
let arr = [{
name: "pink",
age: "17",
info: {
height: 170,
weight: 120
}
}, {
name: "lala",
age: 20,
info: {
height: 160,
weight: 100
}
}]
ul.innerHTML = arr.map((item, i) => {
return `
${item.name} ${item.age}
${arr.map((items,i) => {
return`
<li>他的身高为:${items.info.height},他的体重为:${items.info.weight}
`
}).join("")
}
</ul>
`
}).join("")
var box = document.querySelector(".box")
var ul = document.querySelector("ul");
var weather = {
temperature: "27",
humidity: "63",
info: "阴",
img: "02"
}
var arr0 = []
for (var k in weather) {
arr0.push(weather[k])
}
// console.log(arr0); //拿到数组后,在遍历里面的元素!
var html = ""
html = arr0.map(function (item, i) {
return `
<li>${item}</li>
`
}).join("")
ul.innerHTML = html;
未完待续。。。