UIWebView iOS document.getElementByID不起作用

时间:2011-08-02 14:11:52

标签: javascript iphone objective-c uiwebview

我对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,所有其他行只返回一个空字符串@“”。我的代码有什么问题?如何获取ID为div

hello

1 个答案:

答案 0 :(得分:5)

我发现了问题:这是我的愚蠢拼写错误,getElementById而不是getElementByID