1 回答
TA贡献1893条经验 获得超10个赞
这里我给你做了这个例子,如果你有什么不明白的,就告诉我:)
<!DOCTYPE html>
<html>
<head>
<title>some title</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
#table-container {
height: 300px;
overflow-y: auto;
/*to prevent it from taking the whole width of the body*/
display: inline-block;
}
#table-container table {
font-family: sans-serif;
font-size: 0.7em;
}
#table-container table tr:nth-child(even) {
background-color: #d9d9d9;
}
.fa-star {
color: lightgreen;
}
</style>
</head>
<body>
<!-- give a good names so anyone reads your code can understand it -->
<div id="table-container">
<table></table>
</div>
<script>
// declare it only once to use it anywhere
let table = document.querySelector("#table-container table");
function addTableHeader() {
// simple and better for reading
table.innerHTML += "<tr><th>Category</th><th>Rating</th><th>Price</th><tr>";
}
function addTableRow(dot) {
/* I'm using `template strings` and it's ES6 feature, if you don't know it check this link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals */
table.innerHTML += `<tr><td>${dot.prime_genre}</td><td>${dot.user_rating}</td><td>${dot.price}</td><tr>`;
}
//-- Testing ----------------------------------------------------
addTableHeader();
for(let i = 0;i < 200;i++) {
// I'm just simulating a filled table so you see the result of scrolling
addTableRow({prime_genre: `a${i + 1}`, user_rating: `<i class="fa fa-star"></i>`.repeat(Math.floor(Math.random() * 5 + 1)), price: "$" + (Math.random() * 20 + 10).toFixed(2)});
}
//---------------------------------------------------------------
</script>
</body>
</html>
- 1 回答
- 0 关注
- 60 浏览
添加回答
举报