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
- 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
本文标签: javascriptUIWebView iOS documentgetElementByID does not workStack Overflow
版权声明:本文标题:javascript - UIWebView iOS document.getElementByID does not work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744097083a2590477.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论