
批改狀態(tài):合格
老師批語:看來你是老同學(xué)了, 加油
方法 | 含義 |
---|---|
concat() | 字符串拼裝 |
slice() | 取子串,可以正負(fù)數(shù)取數(shù) |
trim() | 刪除字符串兩邊的空白字符 |
split() | 將字符打散成數(shù)組 |
可以看到空格也算一個(gè)字符串,我們用trim方法刪除/過濾掉空白字符串看看長度
可以看到trim過濾掉了空白字符串,只剩下兩個(gè)實(shí)體字符串了④split將字符打散成數(shù)組
可以看到用split 傳入了@ 把字符串隔開成了兩個(gè)數(shù)組
方法 | 含義 |
---|---|
push() | 尾部添加,入棧 |
pop() | 尾部刪除,出棧 |
unsift() | 在數(shù)組頭部添加 |
shift() | 在數(shù)組頭部刪除 |
join() | 將數(shù)組轉(zhuǎn)為字符串 |
filter() | 對(duì)每個(gè)成員應(yīng)用回調(diào)函數(shù)進(jìn)行處理返回true的成員 |
reduce() | 歸納操作,將多個(gè)成員進(jìn)行統(tǒng)計(jì)后單值返回 |
留言板添加數(shù)字實(shí)時(shí)統(tǒng)計(jì)用的是oninput方法只要值發(fā)生變化時(shí)連續(xù)觸發(fā),不等失去焦點(diǎn),限制長度用的是maxlength屬性限制到100.
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background: lightseagreen;
}
/* from 表單請(qǐng)留言 */
.comment {
margin: 40px auto;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 40px 1fr;
place-items: center;
gap: 5px;
}
.comment > label {
color: white;
font-size: 20px;
}
/*--------------------------*/
/*留言文本框*/
.comment > #content {
width: 40em;
border-radius: 0.5em;
}
#content:hover {
box-shadow: lightcyan 0 0 1em;
transition: 0.6s;
}
/*----------------------------*/
/* 提交按鈕樣式*/
.comment > .submit {
width: 10rem;
height: 2rem;
background: lightcyan;
border: none;
cursor: pointer;
}
.comment > .submit:hover {
box-shadow: 0 0 1em;
}
/* ---------------------------- */
/*留言框樣式*/
.list {
width: 40em;
height: 30em;
margin: 0px auto;
background: white;
font-style: normal;
border-radius: 1em;
}
.list:hover {
box-shadow: lightcyan 0 0 1em;
transition: 0.6s;
}
.list > ul {
padding: 1rem;
background: none;
box-shadow: none;
}
.list > ul:hover {
box-shadow: none;
}
/* 提示*/
.tishi {
color: white;
margin-top: 5px;
}
<!DOCTYPE html>
<html lang="en">
<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>留言板</title>
<style>
@import url(comment.css);
</style>
</head>
<body>
<form action="" class="comment">
<label for="content">請(qǐng)留言:</label>
<textarea
name="content"
id="content"
cols="30"
rows="10"
maxlength="100"
placeholder="請(qǐng)留言不要超過100個(gè)字"
></textarea>
<div class="tishi"></div>
<span class="tishi"></span>
<button class="submit" type="button" name="submit">提交</button>
</form>
<fieldset class="list">
<legend style="font-weight: bolder; font-size: 1.3em">留言框</legend>
<ul class="listm"></ul>
<script>
//留言板功能實(shí)現(xiàn)綁定在留言框
//第一步拿到html里面的表單元素
//拿到form表單
const comment = document.querySelector(".comment");
//拿到文本輸入框
const content = comment.content;
//拿到提交按鈕
const submitBtn = comment.submit;
//拿到列表項(xiàng)
const Slistm = document.querySelector(".listm");
//拿到提示項(xiàng)
const tishi = document.querySelector(".tishi");
//文本提示事件
content.oninput = function () {
if (content.value.length > 0 && content.value.length <= 100) {
tishi.innerHTML = `您還可以輸入${
100 - this.value.trim().length
}個(gè)字符`;
tishi.style.maxlength = "100";
} else {
content.oninput = function () {
tishi.innerHTML = `您還可以輸入${
100 - this.value.trim().length
}個(gè)字符`;
};
}
};
//提交按鈕事件觸發(fā)綁定
submitBtn.onclick = (ev) => {
// trim()發(fā)方法是過濾空格
//創(chuàng)建一個(gè)value變量,并且這個(gè)變量拿到輸入文本框里的文字和過濾空格
let value = content.value.trim();
//做一個(gè)判斷,判斷文本款里的內(nèi)容是否 > 0 或 <=100 個(gè)文字
if (value.length > 0 && value.length <= 100) {
//如果正確就鑲嵌到列表中
//首先創(chuàng)建li列表,然后拿到value的文字
const newComment = document.createElement("li");
//添加li的樣式
newComment.style.listStyle = "none";
newComment.style.borderBottom = "1px solid ";
newComment.style.height = "2em";
newComment.style.margin = "5px";
newComment.textContent = value;
//添加刪除按鈕功能
const deletBtn = document.createElement("button");
deletBtn.textContent = "刪除";
deletBtn.style.width = "3rem";
deletBtn.style.height = "1.2rem";
deletBtn.style.cursor = "pointer";
deletBtn.style.background = "cyan";
deletBtn.style.border = "none";
deletBtn.style.float = "right";
//添加刪除按鈕事件
deletBtn.onclick = function () {
// confirm() 是個(gè)詢問彈窗,里面有確定和取消
if (confirm("是否刪除")) {
//確定是 true 取消是false
//當(dāng)前刪除按鈕的父節(jié)點(diǎn)是li,所以刪除父節(jié)點(diǎn)就可以刪除留言了
this.parentNode.remove();
//刪除要通知客戶
alert("刪除成功");
//設(shè)置焦點(diǎn)
content.focus();
return false;
}
};
//將刪除功能添加到新留言后面
newComment.append(deletBtn);
//將新留言添加到留言框中
Slistm.prepend(newComment);
content.value = null;
//并且通知客戶添加成功
alert("提交成功");
//設(shè)置焦點(diǎn)
content.focus();
} else {
//如果不正確就彈出窗口
alert("沒有內(nèi)容或內(nèi)容超出規(guī)定長度");
//設(shè)置焦點(diǎn)
content.focus();
//跳出判斷
return false;
}
};
</script>
</fieldset>
</body>
</html>
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)