nl2br() 等效于 javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7467840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nl2br() equivalent in javascript
提问by X10nD
Possible Duplicate:
jQuery convert line breaks to br (nl2br equivalent)
Currently I add <BR>
for each evt.which == 13
. Is there a nl2br()
for JavaScript, so I can do away with this evt.which == 13
?
目前我<BR>
为每个evt.which == 13
. 是否有nl2br()
JavaScript 的,所以我可以取消这个evt.which == 13
?
How different is this from php.js
这与 php.js 有何不同
$('#TextArea').keypress(function(evt) {
if (evt.which == 13) {
var range = $('#TextArea').getSelection();
var image_selection = range.text;
$('#TextArea').replaceSelection('<BR>');
$('#TextArea1').html($('#TextArea').val());
}
});
回答by oezi
Take a look at nl2br on php.jswhich seems exactly what you're looking for. Basically, it's:
看看php.js 上的 nl2br,这似乎正是您要找的。基本上,它是:
function nl2br (str, is_xhtml) {
if (typeof str === 'undefined' || str === null) {
return '';
}
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '' + breakTag + '');
}
EDIT:
your example using nl2br()
may be changed like this:
编辑:
您使用的示例nl2br()
可能会更改为:
$('#TextArea').keypress(function(evt){
$('#TextArea1').html(nl2br($('#TextArea').val()));
});
(note that this updates #TextArea1
on every keypress and doesn't change the value of #TextArea
wich is what I think you're looking for, but I might be misunderstanding)
(请注意,这会#TextArea1
在每次按键时更新,并且不会改变#TextArea
我认为您正在寻找的wich的值,但我可能会误解)
EDIT2:
If you want to get the behaviour of your old function (with inserting <br/>
s to #TextArea
) do this:
EDIT2:
如果您想获得旧函数的行为(插入<br/>
s to #TextArea
),请执行以下操作:
$('#TextArea').keypress(function(evt){
$('#TextArea').html(nl2br($('#TextArea').val())); // replace linebreaks first
$('#TextArea1').html($('#TextArea').val()); // copy to #TextArea1
});
回答by xdazz
Here is a function nl2brin php.js.
function nl2br (str, is_xhtml) {
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Philip Peterson
// + improved by: Onno Marsman
// + improved by: Atli Tór
// + bugfixed by: Onno Marsman
// + input by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Brett Zamir (http://brett-zamir.me)
// + improved by: Maximusya
// * example 1: nl2br('Kevin\nvan\nZonneveld');
// * returns 1: 'Kevin<br />\nvan<br />\nZonneveld'
// * example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
// * returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
// * example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
// * returns 3: '<br />\nOne<br />\nTwo<br />\n<br />\nThree<br />\n'
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br ' + '/>' : '<br>'; // Adjust comment to avoid issue on phpjs.org display
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '' + breakTag + '');
}