javascript - 在 Swift 中使用 UIWebView.stringByEvaluatingJavaScriptFromString 删除 html 中带有 <noscript> 标签的所有元素

标签 javascript ios swift uiwebview noscript

我正在尝试在 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/

相关文章:

javascript - react : How to get variable from URL?

objective-c - 如何解决仍然为零的委托(delegate)

ios - 在 iOS 的 React Native 中,我遇到了 ART Lib 问题

ios - UIImagePickerController 和 UINavigationControllerDelegate 实现

ios - 在后台下载图像

javascript - 单击时将类添加到 HTML 列表中的一个且只有一个元素

javascript - 用 Jest 在 javascript 中模拟类原型(prototype)

ios - 如何增加声音的音量并消除 objective-c 中的背景噪音

arrays - 快速编码数组中的不同键

javascript - 如何在 onclick 中传递一些 getElementById