这段代码的效果具体是输入标题和内容,点击发布把消息发布出去,并使最新的消息始终在内容的最上面,代码为:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>微博消息效果</title>
<style>
*{margin:0;padding: 0;}
.warp{
width: 600px;
margin:50px auto 0;
background-color: #ccc;
}
#box{
width: 600px;
height: 340px;
/*background-color: #ccc;*/
position: relative;
/*overflow: hidden;*/
/*margin:50px auto 0;*/
font-family: '微软雅黑';
}
#box .span1{
position: absolute;
font-size: 16px;
line-height: 16px;
top: 10px;
left: 5px;
}
#box .span2{
position: absolute;
font-size: 16px;
line-height: 16px;
top: 50px;
left: 5px;
}
#title{
position: absolute;
width: 460px;
height: 20px;
line-height: 20px;
font-size: 16px;
text-indent: 5px;
left: 70px;
top: 6px;
}
#text{
position: absolute;
width: 460px;
height: 250px;
resize: none;
top: 50px;
left: 70px;
text-indent: 5px;
font-size: 16px;
}
#box #prompt{
position: absolute;
top: 312px;
left: 340px;
}
#prompt1{
position: absolute;
top: 312px;
left: 340px;
display: none;
}
#send{
position: absolute;
height: 25px;
width: 60px;
line-height: 20px;
font-size: 16px;
top: 310px;
right: 68px;
}
#news{
list-style: none;
width: 490px;
margin:10px auto 0px;
padding-bottom: 5px;
}
#news li{
width: 490px;
font-size: 14px;
overflow: hidden;
background-color: #fff;
margin-bottom: 5px;
position: relative;
}
#news li h1{
font-size: 16px;
line-height: 20px;
}
#news li p{
text-indent: 5px;
clear: left;
}
#news li span{
position: absolute;
top: 0px;
right: 0px;
cursor: pointer;
}
#news span:hover{
color: red;
}
</style>
</head>
<body>
<div class="warp">
<div id="box">
<span class='span1'>标题:</span>
<input id="title" type="text">
<span class="span2">内容:</span>
<textarea id="text"></textarea>
<em id="prompt">还可以输入<var id="textnum">200</var>字</em>
<em id="prompt1">你已超出<var id="textnum1"></var>字</em>
<button id="send">发送</button>
</div>
<ul id="news">
<li><h1></h1><span></span>
<p></p>
</li>
</ul>
</div>
<script>
var title=document.getElementById('title');
var text=document.getElementById('text');
var send=document.getElementById('send');
var ul=document.getElementById('news');
var lis=ul.getElementsByTagName('li');
var prompt=document.getElementById('prompt');
var prompt1=document.getElementById('prompt1');
var textnum=document.getElementById('textnum');
var textnum1=document.getElementById('textnum1');
var timer1=null,timer2=null;
send.onclick=function(){
if (text.value==''||title.value=='') {
alert('亲~标题或内容不能为空');return false;
}
lis[0].innerHTML='<h1>'+title.value+'</h1><span>×</span><p>'+text.value+'</p>';
lis[0].children[1].setAttribute('id','close');
var newLi=document.createElement('li');
ul.insertBefore(newLi,lis[0]);
maxheight=lis[1].clientHeight;
lis[1].style.height=0+'px';
var x=0;
var minstep=0;
var maxstep=20;
var change=maxheight/maxstep;
clearInterval(timer1);
timer1=setInterval(function(){
minstep++;
if (minstep>=maxstep) {
clearInterval(timer1);
}
x+=change;
lis[1].style.height=x+'px';
},10)
title.value='';
text.value='';
var close=document.getElementById('close');
for (var i = 0; i < lis.length; i++) {
close.onclick=function(){
var isme=this.parentNode;
var x=this.parentNode.clientHeight;
var minstep=0;
var maxstep=20;
var change=x/maxstep;
clearInterval(timer1);
timer1=setInterval(function(){
minstep++;
if (minstep>=maxstep) {
clearInterval(timer1);
ul.removeChild(isme);
}
x-=change;
isme.style.height=x+'px';
},10)
// ul.removeChild(lis[i]);//不可以,不知道绑定的是第几个。
}
}
}
text.onfocus=function(){
// console.log(prompt.children[0].innerHTML);//children是指带有标签的子节点;
timer2=setInterval(function(){
if(text.value.length<190){
var num=200-text.value.length;
textnum.style.color='black';
// prompt.style.color='black';
textnum.innerHTML=num;//
// prompt.innerHTML='还可以输入<var id="textnum">'+num+'</var>字</em>';
}
if (text.value.length>=190&&text.value.length<=200){
var num=200-text.value.length;
// prompt.style.color='black';
textnum.style.color='red';//为什么不变红呢?因为这他妹的也是一个未来事件!
// prompt.innerHTML='还可以输入<var id="textnum">'+num+'</var>字</em>';
textnum.innerHTML=num;
}
if (text.value.length>200){
var num=text.value.length-200;
// prompt.style.color='red';
prompt.style.display='none';
prompt1.style.display='block';
textnum1.style.color='red';
textnum1.innerHTML=num; }
// console.log(text.value.length);
},50) }
text.onblur=function(){
clearInterval(timer2);
}
</script>
</body>
</html>

这段代码主要运用了一些DOM节点操作的知识,纯属学习之余练手作品,大家可以参考参考。

最新文章

  1. python学习笔记(一)
  2. 渐析java的浅拷贝和深拷贝
  3. 安卓中的数据存储方式以及ContentProvider的简单介绍
  4. 【转】关于B/S架构应用程序的权限设置分析和总结
  5. javascript中的变量、执行环境、作用域
  6. C#(WinForm)实现软件注册
  7. [转载]关于安装Android Studio的一些问题的解决方法
  8. jquery.uploadify+spring mvc实现上传图片
  9. 配置apache使之支持浏览器端的缓存
  10. 11 OptionsMenu 菜单
  11. Ubuntu 16.04 安装垃圾清理工具 BleachBit
  12. wtforms组件使用实例及源码解析
  13. [原创]标记下Kendo使用中的问题, 持续更新中ing.....
  14. laravel 多检索条件列表查询
  15. .NET手记-Autofac进阶(传递注册参数 Passing Parameters to Register)
  16. 11月26日 用seed,预加载种子文件; Case 条件语句。网址的参数如何传递,; Query--自定义scopes
  17. 哈希表(散列表)—Hash表解决地址冲突 C语言实现
  18. TP 框架 ajax[利用异步提交表单]
  19. python基础篇之进阶
  20. 【SSO单点系列】(7):CAS4.0 SERVER通过数据库方式认证用户

热门文章

  1. eclipse使用tomcat进行部署时编译代码不一致的处理
  2. 用实例讲解Spark Sreaming--转
  3. Android自动连接指定的wifi,免密码或指定密码
  4. selenium-webdriver(python) (十三) -- cookie处理
  5. 可视化(番外篇)——SWT总结
  6. 【圣诞特献】Web 前端开发精华文章推荐【系列二十一】
  7. 前端模块化开发之seaJs
  8. ASP.NET MVC+EF在服务端分页使用jqGrid以及jquery Datatables的注意事项
  9. .Net语言 APP开发平台——Smobiler学习日志:用MenuView控件仿钉钉APP的首页菜单
  10. 关于EF的 序列化类型为“XXX”的对象时检测到循环引用。