31 lines
1.4 KiB
HTML
31 lines
1.4 KiB
HTML
<!doctype html>
|
|
<html lang="zh-CN">
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<title>文字统计</title>
|
|
<style>
|
|
* { box-sizing: border-box; }
|
|
body { max-width: 740px; margin: 0 auto; padding: 28px; font: 16px system-ui; color: #18352e; background: #f6fbf8; }
|
|
h1 { font-size: 24px; }
|
|
label { display: block; margin-bottom: 7px; }
|
|
textarea { width: 100%; padding: 12px; border: 1px solid #b6cfc5; border-radius: 8px; font: inherit; resize: vertical; }
|
|
output { display: block; margin-top: 12px; font-weight: 600; }
|
|
</style>
|
|
<h1>文字统计</h1>
|
|
<label for="source">输入文字</label>
|
|
<textarea id="source" rows="8"></textarea>
|
|
<output id="count" for="source" aria-live="polite">0 个字符 · 0 个词 · 0 行</output>
|
|
<script>
|
|
const source = document.getElementById("source");
|
|
const count = document.getElementById("count");
|
|
const segmenter = new Intl.Segmenter("zh-CN", { granularity: "word" });
|
|
source.addEventListener("input", () => {
|
|
const text = source.value;
|
|
const characters = Array.from(text).length;
|
|
const words = Array.from(segmenter.segment(text)).filter((part) => part.isWordLike).length;
|
|
const lines = text ? text.split("\n").length : 0;
|
|
count.textContent = `${characters} 个字符 · ${words} 个词 · ${lines} 行`;
|
|
});
|
|
</script>
|
|
</html>
|