2012-06-14 142 views
0

我試圖獲取UIWebView的視圖中最頂部的元素。在UIWebView的位置獲取HTML元素

我一直試圖

NSString *topElementInView = [self.webView stringByEvaluatingJavaScriptFromString: @"document.elementFromPoint(0, 0);"]; 

但it's不返回任何東西。該代碼在WebView完成加載後運行。

回答

-5
- (void)viewDidLoad { 
js=dic; 
//NSString *js; (.h) 
[self.myWebView loadHTMLString:js baseURL:nil]; 
} 

//委託

- (void)webViewDidFinishLoad:(UIWebView *)webView { 
[myWebView stringByEvaluatingJavaScriptFromString:js]; 
} 
+0

我不明白...... – user1033619

+0

這個代碼轉換成HTML和打開這個字符串中網視圖 –

0

這裏有一個例子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); 
}