I need some help about JavaScript on iPhone UIWebView
;
I have HTML like below:
Example
Since this is a UIWebView
, this is Mobile Safari and you can lose a lot of those branches for obtaining the selection. I would suggest using document.execCommand()
with the "HiliteColor" command, which is built into the browser and works on the whole selection even when it crosses element boundaries:
var sel = window.getSelection();
if (!sel.isCollapsed) {
var selRange = sel.getRangeAt(0);
document.designMode = "on";
sel.removeAllRanges();
sel.addRange(selRange);
document.execCommand("HiliteColor", false, "#ffffcc");
sel.removeAllRanges();
document.designMode = "off";
}