<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
li {
list-style: none;
width: 200px;
background-color: rgb(176, 231, 250);
}
textarea {
resize: none;
width: 300px;
height: 150px;
border: 1px solid blue;
}
</style>
</head>
<body>
<textarea name="" id="" cols="30" rows="10">123</textarea>
<button>发布</button>
<ul>
</ul>
<script>
// 获取元素
var but = document.querySelector('button');
var text = document.querySelector('textarea');
var ul = document.querySelector('ul');
// 注册事件
but.onclick = function () {
if (text.value == '') {
alert('您还没有输入呢?')
return false;
} else {
// 创建元素
var li = document.createElement('li');
li.innerHTML = text.value;
// 添加元素
ul.insertBefore(li, ul.children[0]);
}
}
</script>
</body>
</html>
留言板案例
最新推荐文章于 2026-01-05 16:48:34 发布
该篇文章展示了如何使用HTML、CSS和JavaScript创建一个简单的网页,用户输入文本后点击按钮,将输入内容添加到无序列表中,实现基本的前端交互。
4864

被折叠的 条评论
为什么被折叠?



