如何将插入符号放在空的DOM元素节点中 [英] How to place caret inside an empty DOM Element Node

查看:124
本文介绍了如何将插入符号放在空的DOM元素节点中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将插入符号(折叠的范围)放在空的元素节点内。这个代码应该是有效的,但是它没有。

How can I place the caret (collapsed range) inside an empty element node. This code is supposed to work, but it doesnt.

node = document.getElementById('some-empty-strong-node')
range = document.createRange()
range.selectNodeContents(node)
window.getSelection().addRange(range)

尝试其他技巧,如在元素节点中添加一个空文本节点,或者在节点之间设置范围开始,范围结束在其他地方,然后折叠范围也不会不工作Anoyone有一个想法?

Trying other tricks like adding an empty text node inside the element node or setting the range start inside the node and the range end somewhere else, and then collapsing the range also doesn't work. Anoyone has an Idea?

推荐答案

这是可能的在Firefox和Opera,但不可能在WebKit和IE< = 8。

This is possible in Firefox and Opera, but impossible in both WebKit and IE <= 8.

WebKit有一个长期的错误来解决这个问题: https://bugs.webkit.org/show_bug.cgi?id=15256 为空元素情况和 https://bugs.webkit.org/show_bug.cgi?id=23189 ,用于将插入符号放置在文本节点的开始处的一般情况。最后我听说这可能是几年前的修正。

WebKit has a long-standing bug to fix this: https://bugs.webkit.org/show_bug.cgi?id=15256 for the empty element case and https://bugs.webkit.org/show_bug.cgi?id=23189 for the general case of placing the caret at the start of a text node. Last I heard it's likely to be years before it's fixed.

IE< = 8有各种各样的选择问题,其中只有一个。 IE 9引入了DOM2 Range支持和HTML5 Selection支持,您的代码可能在IE 9中工作(我现在无法测试)。

IE <= 8 has all kinds of issues around selections, of which this is but one. IE 9 introduced DOM2 Range support and HTML5 Selection support and your code may well work in IE 9 (I can't test it right now).

这篇关于如何将插入符号放在空的DOM元素节点中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆