简体   繁体   English

有没有可以保留windows CRLF的HTML元素,即\\r\\n

[英]Is there any HTML element that can preserve windows CRLF, i.e \r\n

  • In my program, I need to render a HTML page where user can pick specific words from the text area and use javascript to capture the selected position indexes(startIndex, endIndex) and perform further process to the source data.在我的程序中,我需要呈现一个 HTML 页面,用户可以在其中从文本区域选择特定的单词并使用 javascript 来捕获选定的位置索引(startIndex、endIndex)并对源数据执行进一步处理。
  • However, there's a use case the source data contains windows CRLF, when it's rendered to a text box(I used span ), the CRLF is lost.但是,有一个用例,源数据包含 windows CRLF,当它呈现到文本框时(我使用了span ),CRLF 丢失了。 So the index is always offset by 1 per line.所以索引总是每行偏移 1。
  • I know potentially we can fix it from the backend, but it would be nice and more consistent if we can preserve the CRLF on the web page.我知道我们可能可以从后端修复它,但是如果我们可以在网页上保留 CRLF 会更好且更一致。

    Example code:示例代码:

 var input = "Hello\\r\\nJavaScript!" document.getElementById("demo").innerHTML = input; var test = document.getElementById("demo").innerHTML console.log('length of input:' + input.length) console.log("length of test: " + test.length)
 span.demo { white-space: pre-wrap; }
 <span class="demo" id="demo">
I want to see input.length==test.length 我想看input.length==test.length

I have explored different fashion of white-space style(including pre), but no luck.我探索了不同white-space风格(包括 pre),但没有运气。

The console.log might not be very clear for this purpose, but you can use Firefox debuger to watch the input variable and see \\r\\n .为此, console.log可能不是很清楚,但您可以使用 Firefox 调试器查看input变量并查看\\r\\n

Thanks a lot非常感谢

The pre element preserves newlines exactly the way you need it! pre元素完全按照您需要的方式保留换行符!

<pre id="test" />

<script>
  document.getElementById("test").innerHTML = "Hello\r\nJavaScript!";
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery:如何覆盖特定元素的默认方法(即html,append)? - jQuery: how to override default methods (i.e html, append) for particular element? 如何在角度指令(即ng-init)r中的字符串插值中使用对象? - How I can use object in string interpolation in angular directive (i.e ng-init)r? javascript如果条件检查字符串带有换行符,即\\ n - javascript if condition check string with newline symbol i.e \n element.setAttibute()方法如何使其在IE中工作? - element.setAttibute() method how to make it work in I.E? chrome和&#39;IE&#39;中的html正文标签限制 - html body tag limitations in chrome and 'I.E' 我想刷新我的 html 页面上可用的所有 div,即按标签,我有 2 个代码如何 imerge - i want to refresh my all div available on my html page i.e by tag and i have 2 codes how can imerge 我可以将.then()与firebaseListObservable一起使用,即this.items = query和.then() - Can I use .then() with firebaseListObservable i.e this.items = query and .then() Lodash:返回其值(即Array)中有给定元素(即字符串)的对象的第一个键 - Lodash : return first key of object whose value(i.e Array) has a given element (i.e string) in it 无法读取带有IE的jQuery加载的属性值 - Can't read attribute value loaded by jQuery with i.e 我可以在任何HTML元素上调用appendChild吗? - Can I call appendChild on any HTML element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM