news 2026/5/15 23:11:08

WEB 作业 即时内容发布前端交互案例

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
WEB 作业 即时内容发布前端交互案例

在这里我做的是喜灰为主题的

代码如下

<htmllang="zh-CN"><head><metacharset="UTF-8"/><metaname="viewport"content="width=device-width, initial-scale=1.0"/><title>灰太郎大王新品发布会</title><style>*{margin:0;padding:0;box-sizing:border-box;}body{background-color:#ffffff00;font-family:"Microsoft Yahei",sans-serif;}ul{list-style:none;}.w{width:900px;margin:20px auto;padding:0 10px;}/* 顶部标题 */.top-title{font-size:18px;color:#4171ff;margin-bottom:15px;line-height:1.2;}/* 输入框区域 */.controls{width:100%;}.controls textarea{width:100%;height:120px;resize:none;border:1px solid #5496ff;border-radius:6px;outline:none;padding:10px 15px;font-size:14px;color:#333;margin-bottom:10px;line-height:1.5;}.controls textarea::placeholder{color:#909399;}/* 字数和发布按钮 */.count-btn-box{float:right;display:flex;align-items:center;gap:8px;}.count-btn-box span{font-size:14px;color:#7ba2ef;}.count-btn-box .useCount{color:#6573f6;font-weight:500;}.count-btn-box button{width:88px;height:32px;border:none;outline:none;background-color:#0084ff;color:#fff;font-size:14px;border-radius:4px;cursor:pointer;transition:background-color 0.2s;}.count-btn-box button:hover{background-color:#0078e7;}.count-btn-box button:disabled{background-color:#a0cfff;cursor:not-allowed;}/* 内容列表 */.contentList{margin-top:40px;width:100%;}.contentList li{padding:20px 0;border-bottom:1px dashed #e6e6e6;position:relative;}.contentList li .info{position:relative;min-height:60px;}.contentList li .info img{width:50px;height:50px;border-radius:50%;object-fit:cover;}.contentList li .info .username{position:absolute;top:5px;left:65px;font-size:15px;font-weight:500;color:#333;}.contentList li .info .send-time{position:absolute;top:30px;left:65px;font-size:12px;color:#999;}.contentList li .content{margin-left:65px;margin-top:10px;font-size:14px;color:#666;line-height:1.6;word-break:break-all;}.contentList li .the_del{position:absolute;right:0;top:20px;font-size:18px;color:#999;cursor:pointer;width:20px;height:20px;line-height:20px;text-align:center;}.contentList li .the_del:hover{color:#f56c6c;}/* 清除浮动 */.clearfix::after{content:"";display:block;clear:both;}</style></head><body><divclass="w"><!-- 顶部标题 --><divclass="top-title">灰太郎大王新品发布会</div><!-- 操作的界面 --><divclass="controls clearfix"><textareaplaceholder="请讲"id="area"maxlength="200"></textarea><divclass="count-btn-box"><spanclass="useCount"id="useCount">0</span><span>/</span><span>200</span><buttonid="send"disabled>发布</button></div></div><divclass="contentList"><ulid="list"></ul></div></div><script>letdataArr=[{uname:'暖羊羊',imgSrc:'./111/1.jpg'},{uname:'沸羊羊',imgSrc:'./111/2.jpg'},{uname:'美羊羊',imgSrc:'./111/3.jpg'},{uname:'懒羊羊',imgSrc:'./111/4.jpg'},{uname:'喜羊羊',imgSrc:'./111/5.jpg'},{uname:'慢羊羊',imgSrc:'./111/6.jpg'},{uname:'灰太狼',imgSrc:'./111/7.jpg'},];// 获取DOM元素constarea=document.getElementById('area');constuseCount=document.getElementById('useCount');constsendBtn=document.getElementById('send');constlist=document.getElementById('list');// 1. 实时字数统计 + 发布按钮状态控制area.addEventListener('input',function(){constcurrentLen=this.value.trim().length;useCount.textContent=currentLen;// 按钮可用条件:字数>0 且 ≤200sendBtn.disabled=currentLen===0||currentLen>200;});// 2. 发布功能sendBtn.addEventListener('click',function(){constcontent=area.value.trim();if(!content)return;// 随机选一个用户constrandomIdx=Math.floor(Math.random()*dataArr.length);constrandomUser=dataArr[randomIdx];// 生成当前时间constnow=newDate();constformatTime=(num)=>num.toString().padStart(2,'0');constyear=now.getFullYear();constmonth=formatTime(now.getMonth()+1);constday=formatTime(now.getDate());consthour=formatTime(now.getHours());constminute=formatTime(now.getMinutes());constsecond=formatTime(now.getSeconds());consttimeStr=`${year}${month}${day}${hour}:${minute}:${second}`;// 创建发布项constli=document.createElement('li');li.innerHTML=`<div class="info"> <img src="${randomUser.imgSrc}" alt="${randomUser.uname}" /> <span class="username">${randomUser.uname}</span> <p class="send-time">发布于${timeStr}</p> </div> <div class="content">${content}</div> <span class="the_del">×</span>`;// 插入到列表顶部list.insertBefore(li,list.firstChild);// 绑定删除事件li.querySelector('.the_del').addEventListener('click',function(){li.remove();});// 清空输入框 + 重置字数 + 禁用按钮area.value='';useCount.textContent='0';sendBtn.disabled=true;});</script></body></html>

运行结果:

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/15 16:51:09

多去跑一些前端大厂面试,你就会发现

去面几家前端大厂&#xff0c;你就会发现&#xff0c;以前纠结八股文 背没背全&#xff0c;思路可能偏了。 当你能流利说出 Vue 和 React 的各种 API&#xff0c;却卡在“React 的 useEffect 与 Vue 的 watch 在 依赖追踪机制上有什么根本不同”时&#xff0c;会突然醒悟——…

作者头像 李华
网站建设 2026/5/13 8:29:11

靠谱的企业微信管理

靠谱的企业微信管理&#xff1a;提升企业沟通与协作效率在数字化转型的浪潮中&#xff0c;企业微信已成为众多企业不可或缺的沟通与协作工具。如何实现高效、安全且可靠的企业微信管理&#xff0c;成为了企业管理者关注的重点。本文将深入探讨企业微信管理的关键要素&#xff0…

作者头像 李华
网站建设 2026/5/13 8:29:10

基于PLC饮料自动化生产线控制系统(设计源文件+万字报告+讲解)(支持资料、图片参考_相关定制)_文章底部可以扫码

基于PLC饮料自动化生产线控制系统(设计源文件万字报告讲解)&#xff08;支持资料、图片参考_相关定制&#xff09;_文章底部可以扫码基于PLC1200的自动化生产线罐装饮料 包含博途V16工程文件&#xff0c;I0接线表&#xff0c;主电路图&#xff0c;控制流程图,配套报告说明文档

作者头像 李华
网站建设 2026/5/13 8:26:37

数据清洗全攻略:让大数据分析更精准的7个步骤

数据清洗全攻略&#xff1a;让大数据分析更精准的7个步骤 一、引言&#xff1a;你离靠谱的数据分析&#xff0c;只差一次“正确的清洗” 1. 一个让数据分析师崩溃的真实场景 上个月&#xff0c;我的朋友小琳&#xff08;某电商公司数据分析师&#xff09;遇到了一件糟心事&a…

作者头像 李华
网站建设 2026/5/10 14:48:16

2026年最新版恒星播放器下载安装全攻略:多格式支持与性能优化详解

一、前言 在多媒体播放领域&#xff0c;恒星播放器因其兼容性强、播放性能稳定、界面简洁等特点&#xff0c;逐渐成为用户在Windows端的热门选择。许多用户在搜索“恒星播放器下载安装”时&#xff0c;往往只找到零散的教程或过时版本。本文将从下载安装方法、功能讲解到系统优…

作者头像 李华
网站建设 2026/5/4 14:24:44

【Flutter × OpenHarmony】跨端开发实现全局Toast提示卡片

文章目录 【Flutter OpenHarmony】跨端开发实现全局Toast提示卡片前言背景Flutter OpenHarmony 跨端开发介绍开发核心代码&#xff08;详细解析&#xff09;1. Toast示例卡片 Widget代码解析&#xff1a; 2. 全局Toast实现方法核心解析&#xff1a; 心得总结 【Flutter Open…

作者头像 李华