突出显示 DOM 范围元素的文本,
我能够突出显示 HTML 页面上的文本(通过 gtkmozembed 呈现),该文本已被选中,如下所示。
var range, sel;
if (window.getSelection) {
sel = window.getSelection();
if (sel.getRangeAt) {
range = sel.getRangeAt(0);
}
document.designMode = "on";
if (range) {
sel.removeAllRanges();
sel.addRange(range);
}
document.execCommand("HiliteColor", false, colour);
document.designMode = "off";
}
嗯,它工作得很好。现在我试图存储有关突出显示文本的信息(startNode,startOffset,endNode,endOffset),下次当我打开同一页面时,突出显示相同的文本。 我能够成功存储信息并在同一页面打开时检索它们。 我正在尝试使用以下代码突出显示文本。
var range = document.createRange();
range.setStart(startNode, startOffset);
range.setEnd(endNode, endOffset);
document.designMode = "on";
range.execCommand("HiliteColor", false, colour);
document.designMode = "off";
但它并没有像我预期的那样工作。谁能帮助我实现所需的目标?谢谢...
I am able to highlight the text on the HTML page(rendered through gtkmozembed), which is selected, like below.
var range, sel;
if (window.getSelection) {
sel = window.getSelection();
if (sel.getRangeAt) {
range = sel.getRangeAt(0);
}
document.designMode = "on";
if (range) {
sel.removeAllRanges();
sel.addRange(range);
}
document.execCommand("HiliteColor", false, colour);
document.designMode = "off";
}
Well,it works very fine.Now i am trying to store the information(startNode, startOffset,endNode, endOffset) about the highlighted text, and next time when i open the same page,highlight the same text.
I am able to successfully store the info and retrieve them when the same page opens.
And i am trying to highlight the text using following code.
var range = document.createRange();
range.setStart(startNode, startOffset);
range.setEnd(endNode, endOffset);
document.designMode = "on";
range.execCommand("HiliteColor", false, colour);
document.designMode = "off";
But it is not working as i am expecting. Can anyone help me to achieve the required? Thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
execCommand
方法是文档的方法,而不是 Range 的方法。另外,hilitecolor
仅适用于 Firefox,因此您应该在 WebKit 和 Opera 中使用backcolor
。更新
已在 IE 9 中修复。
The
execCommand
method is a method of the document, not the Range. Also,hilitecolor
only works in Firefox, so you should fall back to usingbackcolor
in WebKit and Opera.UPDATE
Fixed in IE 9.
此页面应该为您提供有关通过脚本突出显示的所有详细信息。我自己没有这样做过,所以最好使用该页面的建议。
This page should give you all the details about highlighting via script. I haven't done it myself, so it's probably best you use the page's recommendations.