我正在尝试在 Swift 应用程序中解析 html 文件,并且希望删除所有带有 noscript 标记的元素。我试过了,但没用
//webView is a UIWebView with html loaded
webView.stringByEvaluatingJavaScriptFromString("var noscript = document.getElementsByTagName('noscript');var parent = document.getElementsByTagName('div');for (var j in parent) {for(var i in noscript) {parent.removeChild(noscript[i]);}}")
最佳答案
你可能会尝试更像一个js代码
var noscript = document.getElementsByTagName('noscript');
var index = noscript.length;
while(index--) {
noscript[index].parentNode.removeChild(noscript[index]);
}
如果我们查看您的 js 代码,我们可以说三件事
var noscript = document.getElementsByTagName('noscript');
var parent = document.getElementsByTagName('div');
for (var j in parent) {
// 1 - you iterate on parent but don't use the j variable
for(var i in noscript) {
// 2 - parent is an htmlcollection, not an htmlNode, it has no removeChild method
// 3 - i will take for values 0...n but also 'item' and 'length'
parent.removeChild(noscript[i]);
}
}
关于javascript - 在 Swift 中使用 UIWebView.stringByEvaluatingJavaScriptFromString 删除 html 中带有 <noscript> 标签的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32638767/