原生js即可,不用写vue for循环 新建ul元素 id="app" js变量: book = [ {name:'《Vue.js实战》', author: '梁灏'}, {name:'《Javascript语言精粹》', author: 'Douglas Crockford'}, {name:'《Javascript高级程序设计》', author: 'Nicholas C.Zakas'} ]; 业务逻辑: 使用for循环将书名遍历到ul元素 涉及: ul、li元素 for循环 字符串拼接 html动态插入
时间: 2023-05-19 07:04:51 浏览: 81
以下是代码实现:
```javascript
// 获取ul元素
const ul = document.getElementById('app');
// 定义书籍数组
const book = [
{name:'《Vue.js实战》', author: '梁灏'},
{name:'《Javascript语言精粹》', author: 'Douglas Crockford'},
{name:'《Javascript高级程序设计》', author: 'Nicholas C.Zakas'}
];
// 遍历书籍数组,拼接li元素并插入到ul中
for (let i = 0; i < book.length; i++) {
const li = document.createElement('li');
li.textContent = book[i].name + ' - ' + book[i].author;
ul.appendChild(li);
}
```
以上代码使用原生JS实现了将书名遍历到ul元素的业务逻辑,使用for循环遍历书籍数组,拼接li元素并插入到ul中。