使用contentEditable div而不是textarea的缺点是什么?

时间:2022-09-06 12:34:50

Would I be shooting myself in the foot by using a div with attribute contentEditable="true" as a text field rather than a textarea?

我是否会通过使用属性contentEditable =“true”作为文本字段而不是textarea的div来拍摄自己的脚?

2 个解决方案

#1


15  

It would work fine, but it'd be a little bit more difficult than a form, simply because you're going to have to wire up your own logic to make the button's click event track down the correct div, get its content, and then perform the submission. The advantage of a textarea is that the browser takes care of all that for you.

它会工作正常,但它比表单更难一点,因为你将不得不连接自己的逻辑,使按钮的点击事件跟踪正确的div,获取其内容,以及然后执行提交。 textarea的优势在于浏览器可以为您完成所有这些工作。

#2


9  

It's not the same thing. First semantically, the purpose of a textarea is to write/edit plain text whereas with contentEditable you can edit list for instance (see: htmldemo) Second the behavior is also different. For example, in chrome when you test the link below and that you delete all the content you loose the focus (the div element disappear) which is not the expected behavior, or if it is it's idiot.

这不是一回事。首先在语义上,textarea的目的是编写/编辑纯文本,而使用contentEditable,您可以编辑列表(参见:htmldemo)其次,行为也不同。例如,在测试下面的链接时使用chrome,并且删除所有内容,您将失去焦点(div元素消失),这不是预期的行为,或者它是否是白痴。

#1


15  

It would work fine, but it'd be a little bit more difficult than a form, simply because you're going to have to wire up your own logic to make the button's click event track down the correct div, get its content, and then perform the submission. The advantage of a textarea is that the browser takes care of all that for you.

它会工作正常,但它比表单更难一点,因为你将不得不连接自己的逻辑,使按钮的点击事件跟踪正确的div,获取其内容,以及然后执行提交。 textarea的优势在于浏览器可以为您完成所有这些工作。

#2


9  

It's not the same thing. First semantically, the purpose of a textarea is to write/edit plain text whereas with contentEditable you can edit list for instance (see: htmldemo) Second the behavior is also different. For example, in chrome when you test the link below and that you delete all the content you loose the focus (the div element disappear) which is not the expected behavior, or if it is it's idiot.

这不是一回事。首先在语义上,textarea的目的是编写/编辑纯文本,而使用contentEditable,您可以编辑列表(参见:htmldemo)其次,行为也不同。例如,在测试下面的链接时使用chrome,并且删除所有内容,您将失去焦点(div元素消失),这不是预期的行为,或者它是否是白痴。