[英]How to change default behavior of contenteditable html element during clicking enter / return?
I've found out during clicking either enter or return on the contenteditable p element it gets extended with new div or p. 我发现在点击输入或返回可信任的p元素时,它会被新的div或p扩展。 I'd llike it is being extended with only br. 我希望它只用br扩展。 I've tried below page , but it's not working. 我试过下面的页面,但它不起作用。
document.execCommand("defaultParagraphSeparator" , false , "br");
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Editable_content https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Editable_content
document.onreadystatechange = function(){
if(document.readyState == 'complete'){
document.execCommand("defaultParagraphSeparator" , false , "br");
}
}
function public_mode(){
var key = window.event.keyCode;
if (key == 13) {
sendMessage();
}
}
function sendMessage(){
var content = document.getElementById("content");
console.log(content.innerHTML);
}
<p id = "content" contenteditable="true" onkeypress="public_mode()"></p>
//p content is:
Hello world!
Hello world!
//console result is:
Hello world!
Hello world!<div>Hello world!</div>
It actually appears, based on the article you linked, that this is only possible in Firefox 根据您链接的文章,它实际上似乎只能在Firefox中使用
Additionally, Firefox supports non-standard argument, br, for defaultParagraphSeparator since Firefox 55. This is really useful if your web application supports the older Firefox behavior with checking whether the browser is Firefox and unfortunately, you don't have much time to fix your web application for newer Firefox, you can take the older Firefox behavior back with inserting this line when you initializes designMode or contenteditable editor 此外,自Firefox 55以来,Firefox支持defaultParagraphSeparator的非标准参数br。如果您的Web应用程序通过检查浏览器是否为Firefox支持较旧的Firefox行为,这非常有用,不幸的是,您没有太多时间来修复对于较新的Firefox的Web应用程序,您可以在初始化designMode或contenteditable编辑器时插入此行来恢复较旧的Firefox行为
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.