61,112
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>生成10个li</title>
<style type="text/css">
ul{
list-style: none;
padding: 0px;
}
li{
border: 1px solid black;
width: 50px;
height: 50px;
float: left;
line-height: 45px;
text-align: center;
}
.red{
background-color: red;
}
.yellow{
background-color: yellow;
}
.blue{
background-color: blue;
}
.green{
background-color: green;
}
</style>
<script type="text/javascript">
function createLi(){
document.write("<ul>");
for(var i=0;i<10;i++){
if(i%4==0){
document.write("<li class='red'>"+i+"</li>");
}
if(i%4==1){
document.write("<li class='yellow'>"+i+"</li>");
}
if(i%4==2){
document.write("<li class='blue'>"+i+"</li>");
}
if(i%4==3){
document.write("<li class='green'>"+i+"</li>");
}
}
document.write("</ul>");
}
</script>
</head>
<body>
<input type="button" id="" value="生成10个li" onclick="createLi()" />
</body>
</html>
<style type="text/css">
ul{
list-style: none;
padding: 0px;
}
li{
border: 1px solid black;
width: 50px;
height: 50px;
float: left;
line-height: 45px;
text-align: center;
}
.red{
background-color: red;
}
.yellow{
background-color: yellow;
}
.blue{
background-color: blue;
}
.green{
background-color: green;
}
</style>
<script type="text/javascript">
function createLi(){
var htmlCode="<ul>";
for(var i=0;i<10;i++){
if(i%4==0){
htmlCode+="<li class='red'>"+i+"</li>";
}
if(i%4==1){
htmlCode+="<li class='yellow'>"+i+"</li>";
}
if(i%4==2){
htmlCode+="<li class='blue'>"+i+"</li>";
}
if(i%4==3){
htmlCode+="<li class='green'>"+i+"</li>";
}
}
htmlCode+="</ul>";
document.getElementById("div1").innerHTML=htmlCode;
}
</script>
</head>
<body>
<input type="button" id="" value="生成10个li" onclick="createLi()" />
<div id="div1"></div>
</body>
提醒一下:因为document.write()会刷新页面内容,所以该方法尽量少用。