如何在iphone UIWebView中使用JavaScript从html中删除标记
伙计们,我有一个html字符串,如下所示如何在iphone UIWebView中使用JavaScript从html中删除标记,javascript,html,tags,Javascript,Html,Tags,伙计们,我有一个html字符串,如下所示 <html> <head><title></title></head> <body>i wanna remove <span name="Note">this</span> tag</body> </html> 结果是: <html> <head><title></title
<html>
<head><title></title></head>
<body>i wanna remove <span name="Note">this</span> tag</body>
</html>
结果是:
<html>
<head><title></title></head>
<body>i wanna remove tag</body>
</html>
我想去掉标签
如何仅删除标记?您可以尝试:
NSString *str = @"function f(){
var doc = document;
var nodes = doc.getElementsByClassName('Note');
alert(nodes.length); // it returns node count correctly
for(var i = nodes.length - 1;i >= 0;i--){
var node = nodes[i];
if (node){
//node.parentNode.removeChild(node);
node.parentNode.replaceChild(doc.createTextNode(node.innerHTML), node);
}
}
}f();";
但是,如果您得到了嵌套的标记,如foobar
,那么这将导致奇怪的结果如果删除类就足够了,那么您可以这样做:
NSString *str = @"function f(){
var doc = document;
var nodes = doc.getElementsByClassName('Note');
alert(nodes.length); // it returns node count correctly
for(var i = nodes.length - 1;i >= 0;i--){
var node = nodes[i];
if (node){
//node.parentNode.removeChild(node);
node.className = '';
}
}
}f();";
编辑:
这将在电子阅读器中工作,用于将注释写入选定文本。。而且,如果我不能删除我添加的标签,将会有很多
标签。。我需要更多来完全删除已知的标记如果你真的需要删除标记,我将有一个新版本的代码。如果你需要的话,你可以看一看。在这里,我试图创建一个突出显示文本的窗口,并在关闭和打开书籍时检索突出显示的文本。我确实突出显示了文本,但我不知道如何存储和检索它。你能帮我吗
NSString *str = @"function f(){
var doc = document;
var nodes = doc.getElementsByClassName('Note');
alert(nodes.length); // it returns node count correctly
for(var i = nodes.length - 1;i >= 0;i--){
var node = nodes[i];
if (node){
//node.parentNode.removeChild(node);
node.className = '';
}
}
}f();";
NSString *str = @"function f(){
var doc = document;
var aux = doc.createElement('span');
//alert(nodes.length); // it returns node count correctly
for(var nodes = doc.getElementsByClassName('Note'); nodes && (nodes.length > 0); doc.getElementsByClassName('Note')){
var node = nodes[0];
if (node){
aux.innerHTML = node.innerHTML;
for (var el = aux.firstChild, next = el.nextSibling; el; el = next, next = next ? next.nextSibling : null){
var tmp = node.parentNode.insertBefore(el, node);
}
node.parentNode.removeChild(node);
}
}
}f();";