textarea、div、pre中如何使用空白、换行符

我们都知道,html的“容器”元素可以通过white-sapce属性实现对空白、换行符的设置,可参考这篇文章:https://blog.csdn.net/liuxiao723846/article/details/118994673

本文介绍主要的容器元素textarea、div、pre中如何处理空白、换行符。

1、textarea:

textarea元素本身具有white-space:pre-wrap属性。我们看个例子:

<html>
<body>

<textarea id="t1" cols="60" rows="10">
textarea test\ttextarea\ntest\ttextarea\ntest\ttest
</textarea>

<textarea id="t2" cols="30" rows="10">
textarea test	textarea
test	textarea
test	test
</textarea>

<textarea id="t3" cols="30" rows="10"></textarea>

<script type="text/javascript">
    const str = "a\nb\tc\rd"
    document.getElementById("t3").value=str;
</script>
</body>
</html>

效果:

结论:textarea本身具有white-space:pre-wrap属性,所以t2和t3是有空白、换行的效果。

2、div:

看一个例子:

<html>
<style>
div {
	border:1px solid #000;width:160px;height: 150px;margin:5px;
	float:left;display:inline;
}
</style>
<body>

<div id="d1">div test\tdiv\ntest\tdiv</div>
<div id="d2">
div test	div
test	div
</div>
<div id="d3"></div>

<div id="d4" style="white-space: pre">div test\tdiv\ntest\tdiv</div>
<div id="d5" style="white-space: pre">
div test	div
test	div
</div>
<div id="d6" style="white-space: pre"></div>

<div id="d7" contenteditable="true">div test\tdiv\ntest\tdiv</div>
<div id="d8" contenteditable="true">
div test	div
test	div
</div>
<div id="d9" contenteditable="true"></div>
 
<script type="text/javascript">
const str = "a\nb\tc\rd"

document.getElementById("d3").innerHTML=str;
document.getElementById("d6").innerHTML=str;
document.getElementById("d9").innerHTML=str;


</script>
</body>
</html>

效果:

结论:无论是否可编辑的div,都需要手动添加white-sapce属性来实现空白、换行符号效果。

3、pre:

 pre元素本身具有white-space:pre属性。我们看个例子:

<html>
<style>
pre {
	border:1px solid #000;width:160px;height: 150px;margin:5px;
	float:left;display:inline;
}
</style>
<body>

<pre id="p1">pre test\tpre\ntest\tpre</pre>
<pre id="p2">
pre test	pre
test	pre
</pre>
<pre id="p3"></pre>

<pre id="p4" contenteditable="true">pre test\tpre\ntest\tpre</pre>
<pre id="p5" contenteditable="true">
pre test	pre
test	pre
</pre>
<pre id="p6" contenteditable="true"></pre>
 
<script type="text/javascript">
const str = "a\nb\tc\rd"

document.getElementById("p3").innerHTML=str;
document.getElementById("p6").innerHTML=str;
</script>
</body>
</html>

效果

结论:和textarea一样。

可以通过以下步骤来判断textarea一行有多少文字: 1. 获取textarea的文本内容。 2. 创建一个隐藏的div元素,并设置其样式与textarea一致。 3. 将textarea的文本内容复制到隐藏的div元素。 4. 获取隐藏的div元素的高度和行高。 5. 通过高度和行高计算出一行所能容纳的字符数。 下面是一个示例代码,可以实现上述步骤: ```html <textarea id="myTextarea"></textarea> <script> var textarea = document.getElementById("myTextarea"); var hiddenDiv = document.createElement("div"); var styles = getComputedStyle(textarea); // 设置隐藏的div元素的样式 hiddenDiv.style.width = styles.width; hiddenDiv.style.padding = styles.padding; hiddenDiv.style.border = styles.border; hiddenDiv.style.position = "absolute"; hiddenDiv.style.top = "-9999px"; hiddenDiv.style.left = "-9999px"; hiddenDiv.style.whiteSpace = "pre-wrap"; // 保留换行符 // 将隐藏的div元素添加到body document.body.appendChild(hiddenDiv); // 监听textarea的输入事件 textarea.addEventListener("input", function() { // 将textarea的文本内容复制到隐藏的div元素 hiddenDiv.textContent = this.value; // 计算一行所能容纳的字符数 var lineHeight = parseInt(styles.lineHeight); var height = hiddenDiv.clientHeight; var charactersPerLine = Math.floor(textarea.clientWidth / (hiddenDiv.clientWidth / textarea.value.length)); console.log("一行可以容纳" + charactersPerLine + "个字符"); }); </script> ``` 注意,上述代码的计算方法仅适用于文本内容没有换行符的情况。如果需要计算含有换行符的文本内容的行数和每行的字符数,则需要进行更复杂的计算。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值