web(微博发布案例)

示例:

1、检测空白内容

2、发布内容

html:

<!DOCTYPE html>
<html lang="en">
 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="./css/weibo.css">
  <script src="./weibo.js"></script>
</head>
 
<body>
  <div class="w">
    <div class="controls">
      <img src="images/tip.png" alt=""><br>
      <textarea placeholder="说点什么吧..." id="area" cols="30" rows="10" maxlength="200"></textarea>
      <div>
        <span class="useCount">0</span>
        <span>/</span>
        <span>200</span>
        <button id="send">发布</button>
      </div>
    </div>
    <div class="contentList">
      <ul id="list">
      </ul>
    </div>
  </div>
  <script src="./weibo.js"></script>
</body>
 
</html>

css:

* {
    margin: 0;
    padding: 0;
}

ul {
    list-style: none;
}

.w {
    width: 900px;
    margin: 0 auto;
}

.controls textarea {
    width: 878px;
    height: 100px;
    resize: none;
    border-radius: 10px;
    outline: none;
    padding-left: 20px;
    padding-top: 10px;
    font-size: 18px;
}

.controls {
    overflow: hidden;
}

.controls div {
    float: right;
}

.controls div span {
    color: #666;
}

.controls div .useCount {
    color: red;
}

.controls div button {
    width: 100px;
    outline: none;
    border: none;
    background: rgb(0, 132, 255);
    height: 30px;
    cursor: pointer;
    color: #fff;
    font: bold 14px '宋体';
    transition: all 0.5s;
}

.controls div button:hover {
    background: rgb(0, 225, 255);
}

.controls div button:disabled {
    background: rgba(0, 225, 255, 0.5);
}

.contentList {
    margin-top: 50px;
}

.contentList li {
    padding: 20px 0;
    border-bottom: 1px dashed #ccc;
    position: relative;
}

.contentList li .info {
    position: relative;
}

.contentList li .info span {
    position: absolute;
    top: 15px;
    left: 100px;
    font: bold 16px '宋体';
}

.contentList li .info p {
    position: absolute;
    top: 40px;
    left: 100px;
    color: #aaa;
    font-size: 12px;
}

.contentList img {
    width: 80px;
    border-radius: 50%;
}

.contentList li .content {
    padding-left: 100px;
    color: #666;
    word-break: break-all;
}

.contentList li .the_del {
    position: absolute;
    right: 0;
    top: 0;
    font-size: 28px;
    cursor: pointer;
}

js:

    <script>
        let dataArr = [{
            uname: '司马懿',
            imgSrc: '../发布微博案例/images/03.jpg'
        }, {
            uname: '女娲',
            imgSrc: '../发布微博案例/images/03.jpg'
        }, {
            uname: '百里守约',
            imgSrc: '../发布微博案例/images/03.jpg'
        }]
        let useCount = document.querySelector('.useCount')
        let send = document.querySelector('#send')
        let list = document.querySelector('#list')
        area.oninput = function() {
            useCount.innerText = area.value.length
        }
 
        send.onclick = function() {
            if (area.value.trim() == '') {
                alert('请输入合法内容')
            } else {
                let li = document.createElement('li')
                let index = parseInt(Math.random() * dataArr.length)
                li.innerHTML = `
                <div class="info">
                  <img class="userpic" src="${dataArr[index].imgSrc}" >
                  <span class="username">${dataArr[index].uname}</span>
                  <p class="send-time">发布于 ${Date().toLocaleString()}</p>
                </div>
                <div class="content">${area.value}</div>
                <span class="the_del">X</span>`
                list.insertBefore(li, list.children[0])
 
                li.querySelector('.the_del').onclick = function() {
                    list.removeChild(this.parentNode)
                }
            }
 
 
 
            area.value = ''
            useCount.innerText = 0
        }
    </script>

相关推荐

  1. 机器学习---聚类案例

    2024-04-27 08:14:02       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-27 08:14:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-27 08:14:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-27 08:14:02       82 阅读
  4. Python语言-面向对象

    2024-04-27 08:14:02       91 阅读

热门阅读

  1. spring boot中一般如何使用线程池

    2024-04-27 08:14:02       33 阅读
  2. http协商缓存和强缓存

    2024-04-27 08:14:02       31 阅读
  3. 【Go】匿名函数与闭包

    2024-04-27 08:14:02       34 阅读
  4. 每天学习一个Linux命令之sort

    2024-04-27 08:14:02       31 阅读
  5. linux安装PyCharm

    2024-04-27 08:14:02       31 阅读
  6. SQL 之 小技巧总结

    2024-04-27 08:14:02       29 阅读
  7. 机器学习中的K-均值聚类算法及其优缺点

    2024-04-27 08:14:02       36 阅读
  8. 前端面试题合集

    2024-04-27 08:14:02       27 阅读
  9. map与forEach的区别

    2024-04-27 08:14:02       34 阅读
  10. 探索Flutter 3.0:跨平台开发的新越界

    2024-04-27 08:14:02       32 阅读
  11. 【Flutter 面试题】 如何让 AppBar 的标题居中?

    2024-04-27 08:14:02       36 阅读