自动创建标签(博客发表原理)

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css" >
        .my{width: 200px; height: 200px; float: left; margin-left: 100px;
            background-color: #795548; margin-top:150px;}
    </style>
</head>
<body>
<div id="add">
</div>
<script type="text/javascript" src="textJs.js"></script>
</body>
</html>

js

 

function Date(src,title,price,litprice) {
    this.src=src;
    this.title=title;
    this.price=price;
    this.litprice=litprice;
}
var allDate=[
    new Date("a.jpg","水果蛋糕",200,20),//模拟数据库数据
    new Date("a.jpg","水果蛋糕",200,20),
    new Date("a.jpg","水果蛋糕",200,20),
    new Date("a.jpg","水果蛋糕",200,20),
    new Date("a.jpg","水果蛋糕",200,20),
    new Date("a.jpg","水果蛋糕",200,20),
    new Date("a.jpg","水果蛋糕",200,20),
    new Date("a.jpg","水果蛋糕",200,20),
];
function $(id) {
    return document.getElementById(id);//获取父容器方法
}
for(var i=0;i<allDate.length;i++)//动态创建html对象
{
    //console.log(allDate.price);
    var element=document.createElement("DIV");//HTML标签大小写都可以
    //element.innerHTML=allDate[i].price;
    element.className="my";//为动态创建的DIV创建类选择器准备定义样式
    //考虑多种选择器
    var picture = document.createElement("img");
    picture.style.width="200px";
    picture.style.height="200px";
    element.appendChild(picture);
    picture.src=allDate[i].src;
    var title = document.createElement("p");
    title.innerHTML=allDate[i].title;
    //prTitle.innerHTML=allDate[i].price;
    title.style.textAlign="center";
    title.style.color="blue";
    element.appendChild(title);
    var prTitle = document.createElement("p");
    prTitle.innerHTML=allDate[i].price+"&nbsp"+"&nbsp"+"&nbsp"+allDate[i].litprice;
    prTitle.style.textAlign="center";
    element.appendChild(prTitle);
    //element.style.width="100px";
    //element.style.height="100px";
    //element.style.backgroundColor="red";
    $("add").appendChild(element);
}
//扩展使用这种方法可以创建博客的发表文章功能

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值