Files
2024-09-27 02:06:13 +08:00

55 lines
1.3 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>留言字数限制</title>
<style>
body {
margin: 0;
padding: 0;
}
.main {
width: 400px;
height: 200px;
border: 1px solid #000;
position: relative;
}
.main span {
position: absolute;
right: 10px;
bottom: 10px;
}
textarea {
width: 100%;
height: 100%;
resize: none;
}
</style>
</head>
<body>
<div class="main">
<textarea name="" id="" cols="30" rows="10"></textarea>
<span>0/10</span>
</div>
</body>
<script>
var numberLength = 10;
var text = document.querySelector("textarea");
var span = document.querySelector("span");
text.oninput = function () {
if (numberLength - text.value.length > 0) {
var tipsText = `${text.value.length}/剩余${numberLength - text.value.length}`
span.innerText = tipsText;
} else {
text.value = text.value.substring(0,numberLength)
span.innerText = '超过字数限制';
}
}
</script>
</html>