news 2026/8/29 4:00:17

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/8/26 11:17:58

深度剖析SkyWalking:从内核原理到生产级全链路监控实战

1.概述 年底实在是太忙了&#xff0c;所以拖更了一个月&#xff0c;很是抱歉哈~ 言归正传&#xff0c;我们之前已经分享过&#xff1a;从入门到实践&#xff1a;玩转分布式链路追踪利器SkyWalking。这篇文章详细介绍了SkyWalking的核心概念、部署流程与接入实践。还不了解的请…

作者头像 李华
网站建设 2026/8/19 13:47:42

‌2026年职业预警:软件测试从业者如何应对AI对核心技能的重构?

被AI取代的五大传统测试技能‌ AI正以不可逆的速度接管软件测试中‌可标准化、高重复、低创造性‌的执行型任务。以下五类技能&#xff0c;已在2025–2026年进入“高危替代区”&#xff1a; 技能类别典型工作内容AI替代方式替代效率提升企业案例‌手动功能测试‌人工点击界面…

作者头像 李华
网站建设 2026/8/28 12:54:09

2026年CTO最想招的不是程序员,而是“懂业务的测试者”

技术变革下的测试者角色重塑 2026年&#xff0c;人工智能&#xff08;AI&#xff09;和云计算已深度融入软件开发全生命周期&#xff0c;CTO的招聘焦点正经历结构性转变。传统程序员虽仍是技术团队骨干&#xff0c;但CTO们更青睐那些能将业务逻辑与测试实践无缝融合的专业人才…

作者头像 李华
网站建设 2026/8/28 14:55:56

2026生成式AI测试:模型自我验证的技术演进与实践路径

自我验证的必然性挑战 随着生成式AI在代码生成、需求解析等测试场景的深度应用&#xff0c;传统人工验证机制面临三大瓶颈&#xff1a;响应延迟性&#xff08;人工校验滞后于AI输出速度&#xff09;、场景覆盖盲区&#xff08;复杂逻辑路径难以穷尽测试&#xff09;及反馈失真…

作者头像 李华