admin管理员组文章数量:1193347
Line breaks and carriage returns... Must be the most complicated part of coding. (for me)
Have this code in a page (which came from database stored as This from Ricardo\nAnd also a test\nRent 3000.00
):
<td title="This from Ricard
And also a test
Rent 3000.00" style="width:198px;text-align:left">....</td>
Then use this to get the title attribute with 'hidden' \n's
var v = $('#'+i).parent().attr('title'); // i = id of <span> child of <td>
Along the way, the line breaks get lost and using the variable v into a text input box:
This from RicardAnd also a testRent 3000.00
What does it take to preserve the \n's
and have this look instead like below?
Line breaks and carriage returns... Must be the most complicated part of coding. (for me)
Have this code in a page (which came from database stored as This from Ricardo\nAnd also a test\nRent 3000.00
):
<td title="This from Ricard
And also a test
Rent 3000.00" style="width:198px;text-align:left">....</td>
Then use this to get the title attribute with 'hidden' \n's
var v = $('#'+i).parent().attr('title'); // i = id of <span> child of <td>
Along the way, the line breaks get lost and using the variable v into a text input box:
This from RicardAnd also a testRent 3000.00
What does it take to preserve the \n's
and have this look instead like below?
6 Answers
Reset to default 18The line breaks are still there, it is just that the browser tries to render them.
If you still want to see the "\n" try this:
var v = $('td:first-of-type').attr('title').replace(/\n/g, "\\n");
Else, if what you want is NOT to see the "\n" and instead see the line breaks, you could do it this way:
var v = $('td:first-of-type').attr('title').replace(/\n/g, "<br/>");
Check this JSBin
you can use javascript global methods escape()
to encode your string v and unescape() to decode it back for use.
here is your5 fiddle
http://jsfiddle.net/jYNKG/
in your code this line can be like this
var v = escape($('#'+i).parent().attr('title'));
Well, if you put the string into a text input box, there will be no new lines since the text input box is for single-line only text.
The \n
are there - you can try splitting by them:
lines = str.split("\n")
You would need to read this from the database directly. The line endings are being interpreted when the HTML is being generated for the site which is why you see the multi-line td element in the code.
When you go to reference that td element, it spits out the value in a single line. Once this data is lost, you can't recreate it in any manner.
According to the HTML specification all line breaks should be "escaped" aka replayed by 

http://www.w3.org/TR/2006/REC-xml11-20060816/#sec-line-ends
http://www.w3.org/TR/2006/REC-xml11-20060816/#AVNormalize
This question had me curious, so I had to check the source: http://www.w3.org/TR/html401/struct/global.html#adef-title
The W3C is completely silent on how various form of white space within an attribute is treated, so I am force to assume it follows the same rules as HTML.
Under HTML, unless under a <PRE>
element or similar CSS
rule, all white space is collapsed to a single space for rendering.
Thus:
<p>The quick brown
fox
jumped
over the
lazy dog</p>
is rendered the same as:
<p>The quick brown fox jumped over the lazy dog</p>
This HTML behavior, combined with fact that user agents (browsers) may render the TITLE
attribute in any way they wish, makes it likely that most (if not all) such agents would have no reason to preserve end-of-line markers (carriage return
and newline
or \r\n
).
Edit:
If the formatting is important, the title
text could be escaped and put inside a data-*
element. Using a mouse-over, a custom popup box could show the formatted HTML instead.
Edit 2 - Taken from your original question:
<td data-title="This from Ricard<br><br>And also a test<br>Rent 3000.00"
style="width:198px;text-align:left">
....
</td>
You can read more about data-*
attributes here: http://www.javascriptkit.com/dhtmltutors/customattributes.shtml
In short, in some mouseover
code, you can find the text with:
var mytitle = <element>.getAttribute("data-title");
Then use a popup div to display mytitle
本文标签: javascriptHow to preserve a line breakStack Overflow
版权声明:本文标题:javascript - How to preserve a line break - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738409382a2085231.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
\n
and then escape your `\` with `\\`.. – Aman J Commented Sep 15, 2012 at 22:55breaks get lost and using the variable v into a text input box
. I assume he is setting the value of the text-input to the valuev
he got from the td title. – nooitaf Commented Sep 15, 2012 at 23:12\n
. I also removed my answer as it just was a repeat ofKryz
's answer at the end. SO didn't inform me of any new answers so I didn't refresh my page not seeing the other answers. – Nope Commented Sep 15, 2012 at 23:25