[英]cross-browser way to get contenteditable's content
Different browsers generate the elements inside contenteditable
differently. 不同的浏览器在contenteditable
生成不同的元素。 It's especially obvious when you have line breaks, or paste multi-line stuffs in. 当您有换行符或粘贴多行内容时,这一点尤其明显。
In the old days with textarea
, you can simply do $('textarea').val()
to retrieve the content inside, and it's reliable and cross-browser compatible. 在使用textarea
的旧时代中,您只需执行$('textarea').val()
即可检索其中的内容,它是可靠的并且可以跨浏览器兼容。
I wonder is there such universally-agreed method to retrieve content inside a contenteditble
as well, such that it's striped off html tags, and lines are properly separated by \\n
. 我想知道是否有这样一种通用的方法也可以检索contenteditble
内的contenteditble
,从而将其剥离html标签,并用\\n
正确分隔行。 If not, how does Facebook messenger do it reliably? 如果没有,Facebook Messenger如何可靠地做到这一点? Do you need a complicated algorithm with browser detection? 您是否需要使用浏览器检测的复杂算法?
One way to do it would be this: 一种方法是:
var content = document.querySelector('[contenteditable]').textContent
The jury's still out on whether it's universally agreed upon because you would probably have to account for differences between browsers regarding newlines and what not. 关于是否获得普遍认可 ,尚无定论,因为您可能不得不考虑浏览器之间关于换行符的区别,以及没有考虑的区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.