admin管理员组

文章数量:1394544

I am having a really weird bug for UIWebView, I load a very simple html into the UIWebView:

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

and then call 4 lines below:

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')"];

Only the first line, using getElementsByTagName return me the correct body html, all other lines just return me an empty string @"". What can go wrong with my code? How can I get the div that has an id of hello

I am having a really weird bug for UIWebView, I load a very simple html into the UIWebView:

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

and then call 4 lines below:

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')"];

Only the first line, using getElementsByTagName return me the correct body html, all other lines just return me an empty string @"". What can go wrong with my code? How can I get the div that has an id of hello

Share Improve this question asked Aug 2, 2011 at 14:11 vodkhangvodkhang 18.7k11 gold badges78 silver badges111 bronze badges 8
  • Try using a semi-colon at the end of each JS statement. – Evan Mulawski Commented Aug 2, 2011 at 14:14
  • If you switch the top two calls, does helloValue have a value? – Evan Mulawski Commented Aug 2, 2011 at 14:20
  • I tried to ment this line: NSString *body = [self.webview stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].outerHTML"];. It still does not return anything – vodkhang Commented Aug 2, 2011 at 14:22
  • There is no value attribute for an HTMLElement. What are you trying to get there? – Evan Mulawski Commented Aug 2, 2011 at 14:22
  • I am trying to get is the content inside the <div id='hello'>hello world</div>, I tried with value, innerHTML and outerHTML, I am not sure if this is the correct way? – vodkhang Commented Aug 2, 2011 at 14:25
 |  Show 3 more ments

1 Answer 1

Reset to default 5

I figured out the problem: it was my stupid misspelling, getElementById instead of getElementByID

本文标签: javascriptUIWebView iOS documentgetElementByID does not workStack Overflow