html - 在文本区域中显示换行符之间的分隔

标签 html css textarea newline

我想要 HTML5 的可视化表示 <textarea>标记指示何时有“硬”换行而不是换行。例如,给定文本:

    Hello, world\n
    How are you, and this text
    wraps to a new line because
    it's too long.\n
    But this is a proper new line.

我想要一些由换行符分隔的行的视觉指示,例如空格:

    Hello world

    How are you, and this text
    wraps to a new line because
    it's too long.

    But this is a proper new line.

插入一个视觉字符或文本字符串来代替换行符,例如 ¶,也可以工作,即

    Hello world¶
    How are you, and this text
    wraps to a new line because
    it's too long.¶
    But this is a proper new line.

理想情况下,我希望能够使用 CSS 来实现这一点,但我不确定用于这种视觉表示的适当机制(如果它存在的话)是什么。

如果可能的话,我宁愿不必修改文本区域的内容。

我也更喜欢使用“纯”textarea 标签(而不是例如 TinyMCE)。

感谢您的阅读,如有任何想法和建议,我们将不胜感激。

最佳答案

Live Demo

<style type="text/css">
#myform textarea {
    border: 1px solid #000;
    position: absolute;
}
#myform #source {
    background: transparent;
    z-index: 1;
}
#myform #mirror {
    color: rgba(0,0,0,0.5);
    z-index: -1;
}
</style>

<script type="text/javascript">
$("#source").keyup(function() {
    $("#mirror").val($("#source").val().replace(/\n/g,"¶\n"));
});
</script>

<form id="myform">
    <textarea id="source" rows="10" cols="40"></textarea>
    <textarea id="mirror" rows="10" cols="40"></textarea>
</form>

注意:仅在 Firefox 3.6.x 上测试。 (IE 需要一些不同的 CSS 来实现不透明度)

关于html - 在文本区域中显示换行符之间的分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5408950/

相关文章:

php - 使用 PDO 将文本区域 $_POST 发送到 MySQL

javascript - 为 td 添加背景颜色

html - 修复了表格的最后一列

javascript - 带有 HTTP 请求数据的 AngularJs 指令

html - 抓取 CSS 以批量检查响应能力

单击移动设备的 Jquery 触发器,将鼠标悬停在桌面上

html - 下划线表头

html - 如何在第一个字符之前按字母顺序从上到下排列名称

c# - 如何将二进制数组转换为word格式并使用c#将其显示到textarea

html - 文本区域自动增加高度