jQuery text() 函数在 IE 中丢失换行符
这是 jQuery 论坛上经常讨论的问题,但我无法找到适合我的情况的解决方案。
我有一个无序列表,其中包含一些文本元素...用户可以通过 jQuery 创建新的列表项,并将其保存到 SQL 数据库中。他们还可以编辑现有的列表项。此外,由于每个列表项中的文本可能会变得很长,因此他们可以选择“隐藏”每个列表项,以便显示字符串的截断版本。这是由一个自定义 jQuery 插件处理的,该插件会截断超过一定长度的列表项...这是截断插件格式化后每个列表项的样子:
<li id="item_1">
<div class="note">
This is the text that shows when this element is truncated <span style="display: none;" class="truncate_ellipsis">...</span>
<span style="display: inline;" class="truncate_more">This is the text that will be hidden if the user clicks on the 'hide' button</span>
</div>
<div class="toggle_wrapper">
<div class="note_toggle">
<a href="#" class="truncate_more_link">Hide note</a>
</div>
<div style="display: block;" class="note_controls">
<span class="edit_note"><a href="#note_textfield" id="edit_note_1">Edit</a></span> | <span class="delete_note"><a href="#" id="delete_note_1">Delete</a></span>
</div>
</div>
</li>
我遇到的问题是用户单击“编辑”并它获取类为“note”的 div 的内容并将其分配给文本区域。然后用户可以编辑文本并保存。我正在使用以下脚本来获取 div 的内容并将其分配给文本区域:
$('.edit_note a').click(function(event){
// Get the parent li of the 'edit' link that was clicked
var parentLi = $(this).closest('li');
// fade out the li that's being edited
parentLi.fadeOut('fast');
// remove the '...' that shows when the note is truncated
parentLi.find('.truncate_ellipsis').remove();
// find the 'note' div within the li that's being edited
var currentNote = parentLi.find('.note');
// grab the id of this note... used when saving to the DB
var noteId = $(this).attr('id').substr(10);
// Set the current note id variable and editing status
currentNoteId = noteId;
currentNoteStatus = 'edit';
//add the note ID as a hidden field to the text editor form
$('input[name$="note_id"]').val(noteId);
// grab the text from this note and add it to the text area
// (textarea id is 'notes_content')
$('#notes_content').val($.trim(currentNote.text()); // this is the text area
// fade in the text area so the user can edit
$('div.notes_textarea').fadeIn();
});
保存后,我将使用我在网上找到的一个函数将换行符转换为 BR,然后将文本区域的内容分配回相应列表项中的“note”div:
function nl2br (str, is_xhtml) {
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}
这在 firefox / chrome / opera / safari 中都可以正常工作...但是,IE 在通过 jQuery text() 函数获取文本时摆脱了换行符。这在 jQuery 文档的本页注释中进行了讨论:
有些人通过克隆源元素,将其包装在“pre”标签中,然后获取其中的内容并将其放入文本区域,从而取得了成功。这适用于换行符,但也会带来额外的空格、制表符等,看起来有点乱。更好的解决方案似乎是使用 html() jQuery 函数来抓取文本,然后替换 br 来换行,并去掉任何其他 html 格式。
然而,我对 Javascript 很陌生,而且我对正则表达式一窍不通,所以我不知道如何做到这一点,而且我的时间不多了!我需要一个正则表达式,或者只是一些字符串格式化方面的帮助来执行以下操作:
从字符串中删除所有跨度标签,而不删除跨度的内容(我的截断函数添加了一个带有“truncate_more”类的跨度.. . 请参阅上面的 html 代码)
删除 br 的并将它们替换为将在 textarea 元素中正确显示并在浏览器中保持一致的换行符
或者.. 如果有人知道对于这个 IE / textarea / linebreak 问题有更好的解决方法,我真的很感激白痴指南:)
相当多的信息可能是一个简单的解决方案,但我想我会尽力解释这种情况!任何帮助将不胜感激......
编辑:'TheJuice's 答案下面适用于 br 的/ IE 换行问题(谢谢!),但我需要执行类似的正则表达式来摆脱跨度,因为它们只封装了一些文本。我尝试了以下方法,在 Firefox 中似乎工作正常,但在 IE 中跨度仍然存在:
var withBRs = currentNote.html();
var textWithBreaks = withBRs.replace(/\<br\>/gi,'\r');
textWithBreaks = textWithBreaks.replace(/\<.*span.*\>/g, '');
另一个编辑:“TheJuice”也解决了这个问题...忽略我可怕的正则表达式,只要按照他说的去做,如果你发现自己处于同样的境地!感谢所有提供建议的人...
This is quite a talked about problem on the jQuery forums but I can't figure out a solution for my situation.
I have an unordered list with some text elements in them... the user can create new list items via jQuery, which are saved to an SQL database. They can also edit existing list items. Also, because the text in each list item can get quite long, they have the option of 'hiding' each list item so it shows a truncated version of the string. This is handled by a custom jQuery plugin that truncates list items that are over a certain length... here's what each list item looks like once the truncate plugin has formatted it:
<li id="item_1">
<div class="note">
This is the text that shows when this element is truncated <span style="display: none;" class="truncate_ellipsis">...</span>
<span style="display: inline;" class="truncate_more">This is the text that will be hidden if the user clicks on the 'hide' button</span>
</div>
<div class="toggle_wrapper">
<div class="note_toggle">
<a href="#" class="truncate_more_link">Hide note</a>
</div>
<div style="display: block;" class="note_controls">
<span class="edit_note"><a href="#note_textfield" id="edit_note_1">Edit</a></span> | <span class="delete_note"><a href="#" id="delete_note_1">Delete</a></span>
</div>
</div>
</li>
The problem I have is that the user clicks on 'edit' and it takes the contents of the div with the class 'note' and assigns it to a text area. The user can then edit the text, and save it. I am using the following script to grab the contents of the div and assign it to the textarea:
$('.edit_note a').click(function(event){
// Get the parent li of the 'edit' link that was clicked
var parentLi = $(this).closest('li');
// fade out the li that's being edited
parentLi.fadeOut('fast');
// remove the '...' that shows when the note is truncated
parentLi.find('.truncate_ellipsis').remove();
// find the 'note' div within the li that's being edited
var currentNote = parentLi.find('.note');
// grab the id of this note... used when saving to the DB
var noteId = $(this).attr('id').substr(10);
// Set the current note id variable and editing status
currentNoteId = noteId;
currentNoteStatus = 'edit';
//add the note ID as a hidden field to the text editor form
$('input[name$="note_id"]').val(noteId);
// grab the text from this note and add it to the text area
// (textarea id is 'notes_content')
$('#notes_content').val($.trim(currentNote.text()); // this is the text area
// fade in the text area so the user can edit
$('div.notes_textarea').fadeIn();
});
Once it's saved I'm using a function I found online to convert the line breaks to BRs, before assigning the contents of the text area back to the 'note' div within the appropriate list item:
function nl2br (str, is_xhtml) {
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}
This all works fine in firefox / chrome / opera / safari... however, IE gets rid of the line breaks when it grabs the text via the jQuery text() function. This is discussed in the comments of this page on the jQuery documentation:
Some people have had success by cloning the source element, wrapping it in 'pre' tags, then taking the contents of that and putting it in the text area. This works for line breaks but also brings over extra spaces, tabs etc. and it looks a bit of a mess. A better solution seems to be to use the html() jQuery function to grab the text, then replace br's for line breaks, and strip out any other html formatting.
I'm new to Javascript however, and I'm rubbish at regular expressions so I wouldn't have a clue how to do this and I'm running out of time! I need a regular expression, or just some help with string formatting to do the following:
remove all span tags from a string WITHOUT removing the contents of the span (my truncate function adds a span with the class "truncate_more" ... see html code above)
remove br's and replace them with newline characters that will display correctly in a textarea element and be consistent across browsers
OR.. if anyone knows of a better workaround to this IE / textarea / linebreak issue, I'd really appreciate an idiots guide :)
Quite a bit of information for what is probably a simple solution but I thought I'd try to explain the situation as best I could! Any help would be hugely appreciated....
EDIT: 'TheJuice's answer below worked for the br's / IE line breaks issue (thanks!), but I need to carry out a similar regex to get rid of the spans, as they only encapsulate some of the text. I tried the following, which seems to work fine in firefox but in IE the spans remain:
var withBRs = currentNote.html();
var textWithBreaks = withBRs.replace(/\<br\>/gi,'\r');
textWithBreaks = textWithBreaks.replace(/\<.*span.*\>/g, '');
ANOTHER EDIT : 'TheJuice' solved this problem too... ignore my horrific regex and just do what he says if you find yourself in the same situation! Thanks to all who offered suggestions...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一个更简单的解决方案,可以根据使用
或标签将任何 HTML 转换为带有换行符的纯文本。
例如,调用以下方法:
returns:
希望有帮助;)
Here is a far simpler solution to convert any HTML into plain text with line breaks based on the use of
<br>
or<p>
tags.For example, calling the following method:
returns:
Hope that helps ;)
我认为这样的事情对你有用。 http://jsfiddle.net/6qbxn/2/。我在 IE 7 和 IE 7 中对此进行了测试8& FF 4.0b7 并且它按预期工作。
JS:
HTML:
编辑:这解决了你的第二个要点。
I think something like this will work for you. http://jsfiddle.net/6qbxn/2/. I tested this in IE 7 & 8 & FF 4.0b7 and it works as expected.
JS:
HTML:
Edit: This addresses your second bullet point.