0
我对UIWebView
有一个非常奇怪的错误,我将一个非常简单的html加载到UIWebView
:UIWebView 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
尝试使用分号在每个JS语句的结束。 –
它仍然不起作用:( – vodkhang
如果你切换前两个调用,'helloValue'是否有值? –