我试图在 UIWebView 的视图中获取最顶层的元素。
我一直在努力
NSString *topElementInView = [self.webView stringByEvaluatingJavaScriptFromString: @"document.elementFromPoint(0, 0);"];
但它没有返回任何东西。代码在 WebView 加载完成后运行。
我试图在 UIWebView 的视图中获取最顶层的元素。
我一直在努力
NSString *topElementInView = [self.webView stringByEvaluatingJavaScriptFromString: @"document.elementFromPoint(0, 0);"];
但它没有返回任何东西。代码在 WebView 加载完成后运行。
here is an example ViewController.m:
#import "ViewController.h"
@interface ViewController () <UIWebViewDelegate>
@property (nonatomic) UIWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *HTML = @"<!DOCTYPE html> <html> <head> <title></title> <meta http-equiv=\"content-type\" content=\"text/html; charset=“utf-8\"> <!-- Internal CSS --> <style type=\"text/css\"> </style> <!-- Extrernal CSS --> <link rel=\"stylesheet\" href=\"css/normalize.css\"> <!-- jQuery --> <script src=\"http://code.jquery.com/jquery.min.js\" type=\"text/javascript\"></script> <!-- Internal JavaScripts --> <script type=\"text/javascript\"> </script> </head> <body> <a href=\"https://www.google.com/images/srpr/logo11w.png\"> <img src=\"https://www.google.com/images/srpr/logo11w.png\" alt=\"google\"> <p>This is a Google image.</p> </a> </body> </html>";
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.webView];
self.webView.delegate = self;
[self.webView loadHTMLString:HTML baseURL:nil];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *htmlInner = [self.webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
NSLog(@"inner\n%@",htmlInner);
NSString *htmlOuter = [self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"];
NSLog(@"outer\n%@",htmlOuter);
}
- (void)viewDidLoad {
js=dic;
//NSString *js; (.h)
[self.myWebView loadHTMLString:js baseURL:nil];
}
//代表
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[myWebView stringByEvaluatingJavaScriptFromString:js];
}