English 中文(简体)
Java Script Selection
原标题:Making Selection & Adding tag dynamically in JavaScript

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.

这是我的《 Java本法典》,以便获得选定案文:

NSString *SelectedString = [NSString stringWithFormat:@"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];

这种做法是好的,使我回到了选定的案文。

另外,本《联合材料法》增加了一个新的标签:

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>.

我如何解决这一问题?

最佳回答

由于这是“IDWebView,因此,这是“移动式”号,你在获得甄选方面可能失去许多分支机构。 我建议使用<代码>文件>execCommand()和“HiliteColor”指挥部,该指挥部已建在浏览器中,并在整个甄选中工作,即使跨越要素界限:

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";
}
问题回答

暂无回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签