如何从 IE 中的 contentEditable div 获取值
我正在尝试创建一个非常简单的 contentEditable div 输入。然而,我能想到的任何获取输入值的方法都不完全是用户在 IE 中输入的内容,因为空格被折叠了。即“你好世界”变成“你好世界”。但对我来说重要的是,如果可能的话,我能够获取用户输入的文本。我有什么想法可以做到这一点吗?
I'm trying to create a very simple contentEditable div input. However, any way that I can think of to get the value of the input is not exactly what the user typed in IE because whitespace is collapsed. i.e. "hello world" becomes "hello world". But it's important to me that I be able to get the text as the user has typed it if at all possible. Any ideas how I can do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否尝试从 div 读取innerHTML?
Did you try to read the innerHTML from the div?
事实证明,我可以使用innerHTML 很好地获取该值,而无需IE 折叠任何空格。但是,无法设置该值以使 IE 不会折叠空格,这使得单元测试绝对不可能。
It turns out that I can get the value just fine using innerHTML without IE collapsing any spaces. However, there is no way to set the value such that IE doesn't collapse spaces, which makes it absolutely impossible to unit test.
使用古老的“ ”它会按照你想要的方式工作;)
Use the good old " " and it will work as you want ;)
对于您的示例,您是否尝试过innerText,我只是担心使用innerHTML可能会返回一些添加的标记,某些浏览器在操作可编辑内容时可能会添加这些标记。
检查此stackoverflow问题以了解innerHTML和innerText之间的区别:
innerText和html之间的区别
For your example did you try innerText, I'm just afraid that using innerHTML might return some added markup that some browsers might add when manipulating the editable content.
Check this stackoverflow questions for difference between innerHTML and innerText:
Difference between innerText and html