2011-08-02 104 views
0

我对UIWebView有一个非常奇怪的错误,我将一个非常简单的html加载到UIWebViewUIWebView iOS document.getElementByID不起作用

[self.webview loadHTMLString:@"<html><body><div id='hello'>hello world</div></body></html>" baseURL:nil];

然后调用以下4行:

NSString *body = [self.webview stringByEvaluatingJavaScriptFromString: 
        @"document.getElementsByTagName('body')[0].outerHTML"]; 

NSString *helloValue = [self.webview stringByEvaluatingJavaScriptFromString: 
        @"document.getElementByID('hello').value"]; 

NSString *helloOuter = [self.webview stringByEvaluatingJavaScriptFromString: 
      @"document.getElementByID('hello').outerHTML"]; 

NSString *helloInner = [self.webview stringByEvaluatingJavaScriptFromString: 
       @"document.getElementByID('hello').innerHTML"]; 

NSString *helloElement = [self.webview stringByEvaluatingJavaScriptFromString: 
      @"document.getElementByID('hello')"]; 

只有第一行,使用getElementsByTagName返回正确的body html,所有其他行只返回一个空字符串@“”。我的代码有什么问题?如何获得id为hello

div
+0

尝试使用分号在每个JS语句的结束。 –

+0

它仍然不起作用:( – vodkhang

+0

如果你切换前两个调用,'helloValue'是否有值? –

回答

5

我想通了这个问题一个id的div:这是我愚蠢的拼写错误,而不是getElementByIdgetElementByID

相关问题