获取选择&跨多个标签环绕内容
我有一个脚本可以更改所选文本的背景颜色。但是,当跨多个元素/标签选择文本时,我遇到了问题。
我得到的代码是:
var text = window.getSelection().getRangeAt(0);
var colour = document.createElement("hlight");
colour.style.backgroundColor = "Yellow";
text.surroundContents(colour);
输出的错误是:
Error: The boundary-points of a range does not meet specific requirements. =
NS_ERROR_DOM_RANGE_BAD_BOUNDARYPOINTS_ERR
Line: 7
我相信这与 getRange() 函数有关,尽管我不太确定如何继续,因为我是 javascript 的初学者。
还有其他方法可以复制我想要实现的目标吗?
非常感谢。
I've got a script that changes the background colour of text that has been selected. However i'm encountering an issue when the text is selected across multiple elements/tags.
The code that i've got is:
var text = window.getSelection().getRangeAt(0);
var colour = document.createElement("hlight");
colour.style.backgroundColor = "Yellow";
text.surroundContents(colour);
And the error being output is:
Error: The boundary-points of a range does not meet specific requirements. =
NS_ERROR_DOM_RANGE_BAD_BOUNDARYPOINTS_ERR
Line: 7
I believe this is to do with the getRange() function though i'm not too sure how to proceed since I am a beginner at javascript.
Is there any other way I can replicate what I am trying to achieve?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
今天有人问这个问题: 如何突出显示 DOM 的文本Range 对象?
这是我的答案:
以下应该执行您想要的操作。在非 IE 浏览器中,它会打开设计模式,应用背景颜色,然后再次关闭设计模式。
更新
已修复以在 IE 9 中工作。
This question has been asked today: How can I highlight the text of the DOM Range object?
Here's my answer:
The following should do what you want. In non-IE browsers it turns on designMode, applies a background colour and then switches designMode off again.
UPDATE
Fixed to work in IE 9.
好吧,我认为在这种情况下使用 mark.js 库非常好。该库的目的是突出显示 HTML 文档中某个单词的所有实例,但可以通过 filter 选项功能对其进行调整,并且可以通过 each 添加其他 span 属性强>选项功能。
检查此 JSFiddle 示例,以获取突出显示用户选择的完整代码,甚至跨多个 HTML 元素也是如此。
Well I think the use of mark.js library is great in this case. The library's intention is to highlight all instances of a certain word in the HTML document, but it can be tweaked through the filter option function, and additional span attributes can be added through the each option function.
Check this JSFiddle sample for completed code that highlights user's selection, even across multiple HTML elements.