【问题标题】:Remove the node from source of UIWebView从 UIWebView 的源中删除节点
【发布时间】:2015-07-31 09:34:16
【问题描述】:
在我的应用程序中,我在 UIWebView 中打开一个 URL。我想删除页面源中的特定节点。在document.body中,它有,
<div ui-view="navbar" class="ng-scope"><-></div>
<div ui-view="mainPanel" class="ng-scope"><-></div>
我想完全删除具有ui-view="navbar" 的节点。我怎么能这样做?
【问题讨论】:
标签:
ios
objective-c
iphone
swift
uiwebview
【解决方案1】:
您可以使用UIWebView方法stringByEvaluatingJavaScriptFromString:执行任意Javascript
例如:
[webView stringByEvaluatingJavaScriptFromString:@"$('div[ui-view=\"navbar\"]').remove();"];
将使用 jQuery 删除导航栏节点。抱歉,我不熟悉 Angular,所以我不知道等效的说法。
【解决方案2】:
使用document.querySelector 查找节点,然后在其上调用remove()。这不需要加载任何特定的 JavaScript 库(UIWebView 不可能是浏览器太旧而无法支持):
webView.stringByEvaluatingJavaScriptFromString(
"document.querySelector('[ui-view=navbar]').remove();")
或 Objective-C:
(void) [webView stringByEvaluatingJavaScriptFromString:
@"document.querySelector('[ui-view=navbar]').remove();"];