[英]UIWebView JavaScript Highlight - how to Highlight special characters? (utf8 encoding?)
我当时想尽全力解决这个问题,但我还是放弃了寻找。 我不太了解Javascript,但我的UIWebView具有.js高亮功能。
我的问题是,如果包含任何特殊字符(例如“',à等”),它不会突出显示文本。
我正在将NSString解析到.js函数上,这可能会引起麻烦,但我无法弄清楚是否必须解析utf8字符或是否必须在.js内将字符串转换为utf8。
这是我的.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());
}
请让我知道我还能提供什么其他信息。
万一其他人对此感到疑惑,解决方案是一个简单的转义字符。 感谢大家。
editedSearchString = [editedSearchString stringByReplacingOccurrencesOfString:@"á" withString:@"\\á"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.