开发者

Making Selection & Adding tag dynamically in JavaScript

开发者 https://www.devze.com 2023-04-09 23:52 出处:网络
I need some help about JavaScript on iPhone UIWebView; I have HTML like below: <html> <head>

I need some help about JavaScript on iPhone UIWebView;

I have HTML like below:

<html>
   <head>
      <title>Example</title>
   </head>
   <body>
     <span>this example for selection <b>from</b> UIWebView</span>
   </body>
</html>

I want to make a selection, and add <span> tag with color to the selected text in HTML to write a note just like an e-book reader.

This is my JavaScript code to get the selected text:

NSString *SelectedString = [NSString strin开发者_Go百科gWithFormat:@"function getSelText()"
                     "{"
                     "var txt = '';"
                     " if (window.getSelection)"
                     "{"
                     "txt = window.getSelection();"
                     " }"
                     "else if (document.getSelection)"
                     "{"
                     "txt = document.getSelection();"
                     "}"
                     "else if (document.selection)"
                     "{"
                     "txt = document.selection.createRange().text;"
                     "}"
                     "else return;"
                     "alert(txt);"
                     "}getSelText();"];
[webView stringByEvaluatingJavaScriptFromString:SelectedString];

This works good, and returns me the selected text.

Also this JS code for adding a new tag:

NSString *AddSpanTag = [NSString stringWithFormat:@"function selHTML() {"
                 "if (window.ActiveXObject) {"
                 "var c = document.selection.createRange();"
                 "return c.htmlText;"
                 "}"
                 "var nNd = document.createElement(\"span\");"
                 "var divIdName = \'myelementid\';"
                 "var ColorAttr = \"background-color: #ffffcc\";"
                 "nNd.setAttribute(\'id\',divIdName);"
                 "nNd.setAttribute(\'style\',ColorAttr);"
                 "var w = getSelection().getRangeAt(0);"
                 "w.surroundContents(nNd);"
                 "return nNd.innerHTML;"
                 "}selHTML();"];
[webView stringByEvaluatingJavaScriptFromString:AddSpanTag];

My problem is so:

if I select "example" (look at the HTML text) text from WebView, it works, because it doesn't contain any tag.

But if I select "selection from" (look at the HTML text) text from WebView, it's not working, because it starts the tag of <b>, and </b> is out of my selection, then I can't add a new tag between <b> and </b>.

How can I solve this problem?


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";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消