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
- 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 withvalue, innerHTML and outerHTML
, I am not sure if this is the correct way? – vodkhang Commented Aug 2, 2011 at 14:25
1 Answer
Reset to default 5I figured out the problem: it was my stupid misspelling, getElementById
instead of getElementByID
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744097083a4558140.html
评论列表(0条)