老师看一下这样写麻烦不,可以简化吗?

来源:2-4 编程练习

Azathouth

2019-11-10 15:31:29

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>模版字符串</title>
</head>

<body>
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
    <script type="text/javascript">
const tmpl = function() {
return {
title: "前端",
data: [{
title: '布局基础',
date: ["html", "css"]
}, {
title: '网页特效',
date: ["javascript", "jquery"]
}, {
title: '框架',
date: ["bootstrap", "vue"]
}]
}
}
const {title,data:dataMsg} = tmpl();
let arr = [];
arr.push(`<caption>${title}</caption>`);
dataMsg.forEach(function({title,date}){
let dates = "";
for(let i=0;i<date.length;i++){
dates += `<td>${date[i]}</td>`;
}
arr.push(
`
            
            <tr>
                <td>${title}</td>
                ${dates}
            </tr>
            `
)
});
let table = document.createElement("table");
table.innerHTML = arr.join("");
document.body.appendChild(table);
table.border = "1px solid black";
table.style.textAlign = "center";
</script>
</body>
</html>


写回答

1回答

樱桃小胖子

2019-11-10

同学你好,代码实现的是正确的,也很简洁,继续加油,祝学习愉快!

0

0 学习 · 10739 问题

查看课程