JQuery 的添加与渲染功能

//前端页面Html代码

<body>
    <div class="box">
        <div class="top">
            <ul>
                <li><img src="img/1.jpg" alt=""></li>
                <li><img src="img/2.jpg" alt=""></li>
                <li><img src="img/3.jpg" alt=""></li>
                <li><img src="img/4.jpg" alt=""></li>
            </ul>
        </div>
        <div class="middle">
            <span>用户名:</span><input type="text" id="inp1" placeholder="请输入用户名">
            <br>
            <span>发表内容</span>
            <br>
            <textarea name="" id="content" cols="50" rows="13" maxlength="150" style="margin-left: 20px;"></textarea>
            <br/>
            <button class="custom-btn btn-2" id="add">添加</button>
        </div>
        <div class="footer">

        </div>
    </div>
</body>

//css代码

@charset "UTF-8";
*{
    margin:0;
    padding:0;
}
img{
    width: 80px;
    height: 82px;
}
.box{
    width:800px;
    border: 1px solid blue;
    margin:50px auto;
}
.box li{
    float:left;
    list-style:none;
    margin:20px;
}
span{
    text-align: center;
    display: inline-block;
    width: 80px;
    height: 50px;
}
.top{
    width: 800px;
    height: 130px;
}
.middle input{
    width: 200px;
    height: 30px;
    border: 1px blue solid;
    border-radius: 5px;
}
.footer{
    width: 800px;
}
.footer li{
    line-height: 80px;
    width: 150px;
    height: 82px;
}
.custom-btn {
    width: 90px;
    height: 40px;
    color: #fff;
    border-radius: 50px;
    padding: 10px 25px;
    font-family: 'Lato', sans-serif;
    font-weight: 500;
    font-size: 16px;
    background: transparent;
    cursor: pointer;
    transition: all 0.3s ease;
    position: relative;
    display: inline-block;
    box-shadow: inset 2px 2px 2px 0px rgba(255,255,255,.5),
    inset -7px -7px 10px 0px rgba(0,0,0,.1),
    7px 7px 20px 0px rgba(0,0,0,.1),
    4px 4px 5px 0px rgba(0,0,0,.1);
    text-shadow:  2px 2px 3px rgba(255,255,255,.5),
    -4px -4px 6px rgba(116, 125, 136, .2);
    outline: none;
    margin: 20px;
}
.btn-2 {
    background-color: #e8d1ff;
    color: rgba(96,9,240, .5);
    border: none;
}
.btn-2:before {
    height: 0%;
    width: 2px;
}
.btn-2:hover {
    box-shadow:  4px 4px 6px 0 rgba(255,255,255,.5),
    -4px -4px 6px 0 rgba(116, 125, 136, .5),
    inset -4px -4px 6px 0 rgba(255,255,255,.2),
    inset 4px 4px 6px 0 rgba(0, 0, 0, .2);
}

  //js代码

<script>
    //选取照片
    var imgSrc;
    var arr =[];
   $(".top>ul>li").click(function () {
       $(this).css('border','1px solid black').siblings().css('border', 'none');
       imgSrc=$(this).children()[0].src;
       console.log(imgSrc)
       }
    )
    //添加
    $('#add').click(function () {
        if ($("#inp1").val() === ''|| $('#content').val() === ''){
            alert('用户或者内容不能为空');
            return;
        }
        let obj ={
            id : new Date().getTime(),
            user:$("#inp1").val(),
            cont:$('#content').val(),
            img:imgSrc,
            shelfTime: new Date().toLocaleTimeString()
        }
        arr.push(obj);
        showFun();
    })
   function showFun() {
        $('.footer')[0].innerHTML='';
        let str ='';
        $.each(arr,function (i,item) {
            str+=`
                <ul>
                    <li><img src="${item.img}" alt=""></li>
                    <li>${item.user}:${item.cont}</li>
                    <li>${item.shelfTime}</li>
                    <li><button>删除</button></li>
            </ul>
            `
        })
       $('.footer')[0].innerHTML=str;
    }
</script>

  //这是国庆来了做的一个小项目,发表于此。后续还会改进。

上一篇:XAMPP简介、安转、使用


下一篇:【JavaScript】开始编写 JavaScript