简体   繁体   English

如何在单击enter / return期间更改contenteditable html元素的默认行为?

[英]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.

相关问题 如何在各种浏览器中输入后更改contenteditable块的行为 - how to change behavior of contenteditable blocks after on enter pressed in various browsers 在输入时验证 contenteditable 而不是点击 - Validate contenteditable on enter and not on clicking HTML contenteditable元素上的onkeyup事件返回未定义 - onkeyup event on HTML contenteditable element return undefined 将重点更改为单击输入javascript html表单 - change focus on clicking enter javascript html form 如何从contenteditable span元素的开头删除enter(换行符)? - How to remove enter (linebreak) from the beginning of contenteditable span element? 如何在单击元素时将contenteditable设置为true - How can I set the contenteditable true on clicking the element 在HTML文本输入元素中按Enter键时的默认行为 - Default behavior when press enter in HTML text input elements HTML 表单中“返回”键的默认行为是什么? - What is the default behavior of the 'return' key in an HTML form? 如何在HTML元素上创建焦点项目,按Enter键可触发其行为(如单击侦听器) - How to make a focused item on an HTML element have its behavior (like click listener) be triggered by pressing enter key 如何更改行为,在WYMEDITOR中按下Enter / Backspace键 - How to change behavior enter/backspace pressed in WYMEDITOR
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM