[英]UIWebView iOS document.getElementByID does not work
我的UIWebView
有一个非常奇怪的错误,我将一个非常简单的html加载到UIWebView
:
[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返回正确的html正文,其他所有行仅返回一个空字符串@“”。 我的代码有什么问题? 我如何获取具有hello
ID的div
我发现了问题:这是我的愚蠢拼写错误,是getElementById
而不是getElementByID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.