【问题标题】:UIWebView iOS document.getElementByID does not workUIWebView iOS document.getElementByID 不起作用
【发布时间】:2011-08-02 14:11:52
【问题描述】:

我对@9​​87654321@ 有一个非常奇怪的错误,我将一个非常简单的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 为 hellodiv

【问题讨论】:

  • 尝试在每个 JS 语句的末尾使用分号。
  • 如果切换前两个调用,helloValue有值吗?
  • 我试图评论这一行:NSString *body = [self.webview stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].outerHTML"];。它仍然没有返回任何东西
  • HTMLElement 没有 value 属性。你想达到什么目的?
  • 我想得到的是<div id='hello'>hello world</div>里面的内容,我试过value, innerHTML and outerHTML,我不确定这是否正确?

标签: javascript iphone objective-c uiwebview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-29
    • 2013-12-24
    • 1970-01-01
    • 2018-08-31
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    相关资源
    最近更新 更多