分析
-
点击发布按钮,动态创建一个li元素,放入文本框的内容和删除按钮,并且添加到ul中
-
点击删除按钮,可以删除当前的微博留言
-
使用slideUp()和slideDown()实现缓动的添加和删除留言
代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
position: relative;
margin: 100px auto;
border: 1px solid;
width: 700px;
text-align: center;
padding: 20px;
}
.txt {
width: 400px;
resize: none;
}
.btn {
position: absolute;
bottom: 10px;
right: 10px;
cursor: pointer;
}
ul {
margin: 0;
padding: 0;
list-style: none;
width: 350px;
text-align: left;
left: 50%;
transform: translate(50%, 0);
}
ul li {
line-height: 25px;
border-bottom: 1px dashed #cccccc;
display: none;
}
ul li a {
float: right;
}
</style>
</head>
<body>
<div class="box" id="weibo">
<span>微博发布</span>
<textarea name="" class="txt" id="" cols="30" rows="10"></textarea>
<button class="btn">发布</button>
<ul></ul>
</div>
<script src="../js/jQuery.js"></script>
<script>
$('.btn').on('click', function() {
var li = $('<li></li>');
li.html($('.txt').val() + "<a href='javascript:;'>删除</a>");
$('ul').prepend(li);
li.slideDown();
$('.txt').val("");
})
$('ul').on('click', "a", function() {
$(this).parent().stop().slideUp(function() {
$(this).remove();
});
})
</script>
</body>
</html>