![](/img/trans.png)
[英]document.execCommand of insertText not working in IE
[英]IE Edge: Formatting Issue document.execCommand('inserttext', true, content)
这是针对此问题的 JS 小提琴: https : //jsfiddle.net/2ds5mor4/1/
在 chrome 和 IE Edge 中打开 JS fiddle。 在文本区域粘贴 (ctrl+V),您将看到未在 IE Edge 中格式化的文本,而相同的用例在 Chrome 浏览器中运行良好。
$('[contenteditable]').on('paste', function(e) {
var content = "Dear,\r\nIf you love reading books, then we have the best match for your reading choices.\r\nwe have 'The Sunflower Tales' as a \r\ncollection of 40 love poems for you and your loved ones.\r\nAbout our Book:\r\n\r\nThe Sunflower Tales is a collection of Love and Life poems penned down by a young commerce graduate. It isn't about selling a Book, it is more about making a space in the hearts of its reader's with time.";
e.preventDefault();
document.execCommand('inserttext', true, content);
});
我已经使用上述函数粘贴和预定义粘贴文本并使用 inserttext execCommand 插入。
谁能告诉我如何保持这种格式?
我用 MS Edge 42.17134.1098.0 测试了这个问题,我发现它给出了与 Chrome 相同的结果。
代码:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script> $(document).ready(function(){ $('[contenteditable]').on('paste', function(e) { var content = "Dear,\\r\\nIf you love reading books, then we have the best match for your reading choices.\\r\\nwe have 'The Sunflower Tales' as a \\r\\ncollection of 40 love poems for you and your loved ones.\\r\\nAbout our Book:\\r\\n\\r\\nThe Sunflower Tales is a collection of Love and Life poems penned down by a young commerce graduate. It isn't about selling a Book, it is more about making a space in the hearts of its reader's with time."; e.preventDefault(); document.execCommand('inserttext', true, content); }); }); </script> <style> div[contenteditable] { border: 1px solid #000; padding: 10px; } div[contenteditable]:hover { background: #ff0; } </style> </head> <body> <div contenteditable="true"> Paste here </div> </body> </html>
在这里查看我的测试结果。
我建议你再做一次测试,看看结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.