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>
.
我如何解决这一问题?