admin管理员组文章数量:1026714
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...
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...
Share Improve this question asked May 3, 2010 at 8:38 ganapatiganapati 6252 gold badges12 silver badges24 bronze badges 1- duplicate of highlight the text of the DOM range element, – Bergi Commented Aug 3, 2012 at 6:43
2 Answers
Reset to default 7The execCommand
method is a method of the document, not the Range. Also, hilitecolor
only works in Firefox, so you should fall back to using backcolor
in WebKit and Opera.
UPDATE
Fixed in IE 9.
function makeEditableAndHighlight(colour) {
var sel = window.getSelection();
var range = null;
if (sel.rangeCount && sel.getRangeAt) {
range = sel.getRangeAt(0);
}
document.designMode = "on";
if (range) {
sel.removeAllRanges();
sel.addRange(range);
}
// Use HiliteColor since some browsers apply BackColor to the whole block
if (!document.execCommand("HiliteColor", false, colour)) {
document.execCommand("BackColor", false, colour);
}
document.designMode = "off";
}
function highlight(colour) {
var range, sel;
if (window.getSelection) {
// IE9 and non-IE
try {
if (!document.execCommand("BackColor", false, colour)) {
makeEditableAndHighlight(colour);
}
} catch (ex) {
makeEditableAndHighlight(colour)
}
} else if (document.selection && document.selection.createRange) {
// IE <= 8 case
range = document.selection.createRange();
range.execCommand("BackColor", false, colour);
}
}
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 remendations.
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...
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...
Share Improve this question asked May 3, 2010 at 8:38 ganapatiganapati 6252 gold badges12 silver badges24 bronze badges 1- duplicate of highlight the text of the DOM range element, – Bergi Commented Aug 3, 2012 at 6:43
2 Answers
Reset to default 7The execCommand
method is a method of the document, not the Range. Also, hilitecolor
only works in Firefox, so you should fall back to using backcolor
in WebKit and Opera.
UPDATE
Fixed in IE 9.
function makeEditableAndHighlight(colour) {
var sel = window.getSelection();
var range = null;
if (sel.rangeCount && sel.getRangeAt) {
range = sel.getRangeAt(0);
}
document.designMode = "on";
if (range) {
sel.removeAllRanges();
sel.addRange(range);
}
// Use HiliteColor since some browsers apply BackColor to the whole block
if (!document.execCommand("HiliteColor", false, colour)) {
document.execCommand("BackColor", false, colour);
}
document.designMode = "off";
}
function highlight(colour) {
var range, sel;
if (window.getSelection) {
// IE9 and non-IE
try {
if (!document.execCommand("BackColor", false, colour)) {
makeEditableAndHighlight(colour);
}
} catch (ex) {
makeEditableAndHighlight(colour)
}
} else if (document.selection && document.selection.createRange) {
// IE <= 8 case
range = document.selection.createRange();
range.execCommand("BackColor", false, colour);
}
}
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 remendations.
本文标签: javascripthighlight the text of the DOM range element Stack Overflow
版权声明:本文标题:javascript - highlight the text of the DOM range element, - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1741565963a1875860.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论