can't get new lines of selected text on IE11

倖福魔咒の 提交于 2019-12-11 00:45:18

问题


I am trying to replace some text inside a div using a modal window with a form input but for some reason I can't get it to work on IE11. I've realized that it skips new lines when selecting a piece of text containing a <br>

Here is a working example:

http://jsfiddle.net/8VdE9/3/

The funny thing is that I am using the same code in a different page and it gets the new lines fine, which makes me think that I am missing something.

var isIE11 = !!navigator.userAgent.match(/Trident.*rv\:11\./);

range = window.getSelection().getRangeAt(0);
if(!isIE11){
    text = new XMLSerializer().serializeToString(range.cloneContents());
}
else{
    text = range;
}
text = new String(text);
var textBr = nl2br(text.toString());

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');
}

Any help would be very appreciated.

Regards

来源:https://stackoverflow.com/questions/22093776/cant-get-new-lines-of-selected-text-on-ie11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!