从 UIWebView 的源中删除节点

Remove the node from source of UIWebView

在我的应用程序中,我在 UIWebView 中打开一个 URL。我想删除页面源中的特定节点。在 document.body 中,它有

<div ui-view="navbar" class="ng-scope"><-></div>
<div ui-view="mainPanel" class="ng-scope"><-></div>

我想完全删除具有 ui-view="navbar" 的节点。我该怎么做?

您可以使用UIWebView方法stringByEvaluatingJavaScriptFromString:

执行任意Javascript

例如:

[webView stringByEvaluatingJavaScriptFromString:@"$('div[ui-view=\"navbar\"]').remove();"];

将使用 jQuery 删除导航栏节点。抱歉,我不熟悉 Angular,所以我不知道等效的语句。

使用document.querySelector找到节点,然后在其上调用remove()。这不需要加载任何特定的 JavaScript 库(UIWebView 浏览器不可能太旧而无法支持它):

webView.stringByEvaluatingJavaScriptFromString(
  "document.querySelector('[ui-view=navbar]').remove();")

或Objective-C:

(void) [webView stringByEvaluatingJavaScriptFromString:
        @"document.querySelector('[ui-view=navbar]').remove();"];