2011-08-02 105 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

相關問題