news 2026/6/15 20:03:03

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/6/15 19:33:30

孤能子视角:“联结主义(神经网络)认知心理学“

第一步&#xff1a;分析“作者”——联结主义/神经网络范式&#xff08;作为对经典认知范式的“生物学启发的逆袭”孤能子&#xff09;启动&#xff1a;三力逼问&#xff0c;定位张力1. 零预设&#xff1a;不预设联结主义是“真正的智能模型”或“数学黑箱”&#xff0c;视其为…

作者头像 李华
网站建设 2026/6/9 19:46:26

吐血推荐!专科生必备9款AI论文写作软件测评

吐血推荐&#xff01;专科生必备9款AI论文写作软件测评 2026年专科生AI论文写作工具测评&#xff1a;为何需要这份榜单&#xff1f; 随着人工智能技术的不断进步&#xff0c;越来越多的专科生开始借助AI工具提升论文写作效率。然而&#xff0c;面对市场上琳琅满目的AI写作软件&…

作者头像 李华
网站建设 2026/6/15 13:39:58

基于Springboot的克州旅游网站的设计与实现

文章目录 详细视频演示项目介绍技术介绍功能介绍核心代码系统效果图源码获取 详细视频演示 文章底部名片&#xff0c;获取项目的完整演示视频&#xff0c;免费解答技术疑问 项目介绍 克孜勒苏柯尔克孜自治州&#xff08;简称“克州”&#xff09;位于新疆西南部&#xff0c;拥…

作者头像 李华
网站建设 2026/6/15 19:58:49

2025年CRM系统选型手册:主流厂商能力横向对比及深度解析

引言在数字化转型背景下&#xff0c;CRM&#xff08;客户关系管理&#xff09;已从“工具”升级为“企业增长引擎”。其核心价值在于通过标准化流程提升效率、全视图客户理解驱动个性化运营、移动化能力适配外勤场景、数据驱动优化绩效。本文选取8个主流CRM品牌&#xff08;超兔…

作者头像 李华
网站建设 2026/5/10 6:01:57

Python RPA从零到实战:一份为期100天的系统培训大纲

前言&#xff1a;为何需要一份100天的RPA学习计划 在数字化转型浪潮中&#xff0c;机器人流程自动化&#xff08;RPA&#xff09;已成为提升效率、降低错误率的核心技术。Python&#xff0c;凭借其简洁的语法、强大的生态库和极高的可扩展性&#xff0c;已成为构建RPA解决方案的…

作者头像 李华
网站建设 2026/6/15 13:26:06

科视Christie在ISE 2026诠释热忱与机遇的交融魅力

互动演示与新品首发&#xff0c;展现视觉叙事的力量 西班牙巴塞罗那&#xff08;2026年1月28日&#xff09;&#xff1a;科视Christie 再度回归ISE展会&#xff0c;展示其最新技术如何开启全新可能&#xff0c;通过强大的叙事能力实现出色的体验。在2026年2月3日至6日于巴塞罗那…

作者头像 李华