[英]UIWebView JavaScript Highlight - how to Highlight special characters? (utf8 encoding?)
I was trying to wrap my head around this but I kind of gave up searching. 我当时想尽全力解决这个问题,但我还是放弃了寻找。 I don't know much about Javascript but I have a .js highlight function for my UIWebView.
我不太了解Javascript,但我的UIWebView具有.js高亮功能。
My problem is that it does not Highlight text if it includes any special characters like: "',à etc." 我的问题是,如果包含任何特殊字符(例如“',à等”),它不会突出显示文本。
I am parsing a NSString onto the .js function which probably is causing the trouble but I can't figure out if I have to parse a utf8 char or if I have to convert the string to utf8 inside my .js. 我正在将NSString解析到.js函数上,这可能会引起麻烦,但我无法弄清楚是否必须解析utf8字符或是否必须在.js内将字符串转换为utf8。
Here is my .js code 这是我的.js代码
function MyApp_HighlightAllOccurencesOfStringForElement(element,keyword) {
if (element) {
if (element.nodeType == 3) { // Text node
while (true) {
var value = element.nodeValue; // Search for keyword in text node
var idx = value.toLowerCase().indexOf(keyword);
if (idx < 0) break; // not found, abort
var span = document.createElement("span");
var text = document.createTextNode(value.substr(idx,keyword.length));
span.appendChild(text);
span.setAttribute("class","MyAppHighlight");
span.style.backgroundColor="#C4B695";
span.style.color="black";
text = document.createTextNode(value.substr(idx+keyword.length));
element.deleteData(idx, value.length - idx);
var next = element.nextSibling;
element.parentNode.insertBefore(span, next);
element.parentNode.insertBefore(text, next);
element = text;
span.scrollIntoView();
MyApp_SearchResultCount++; // update the counter
}
} else if (element.nodeType == 1) { // Element node
if (element.style.display != "none" && element.nodeName.toLowerCase() != 'select') {
for (var i=element.childNodes.length-1; i>=0; i--) {
MyApp_HighlightAllOccurencesOfStringForElement(element.childNodes[i],keyword);
}
}
}
}
}
// the main entry point to start the search
function MyApp_HighlightAllOccurencesOfString(keyword) {
// MyApp_RemoveAllHighlights();
MyApp_HighlightAllOccurencesOfStringForElement(document.body, keyword.toLowerCase());
}
Please let me know what other information I can provide. 请让我知道我还能提供什么其他信息。
in case anyone else was wondering about that, the solution was a simple escape character. 万一其他人对此感到疑惑,解决方案是一个简单的转义字符。 Thanks everyone.
感谢大家。
editedSearchString = [editedSearchString stringByReplacingOccurrencesOfString:@"á" withString:@"\\á"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.