【问题标题】:Get iframe src from webview stringByEvaluatingJavaScriptFromString从 webview stringByEvaluatingJavaScriptFromString 获取 iframe src
【发布时间】:2016-04-20 03:45:22
【问题描述】:

我有一个 URL 正在加载到 webview 中,加载完成后,我想获取 HTML 中的 iframe src,并使用 iframe src 重新加载 webview。

<iframe frameborder="0" allowtransparency="true" marginheight="0" marginwidth="0" scrolling="no" src="https://example.com/abc"></iframe>

在我的 iOS 代码中,在 webViewDidFinishLoad 中,我正在尝试获取 iframe 使用

webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('iframe').attr('src')"];

这个 javascript 在 chrome 控制台中运行,但它不适合我。

你能帮帮我吗?

【问题讨论】:

    标签: ios objective-c uiwebview stringbyevaluatingjavascr


    【解决方案1】:

    我很惊讶 document.getElementById 在 iframe 上实际上没有 id 属性时有效。试试这个:

    document.getElementsByTagName('iframe')[0].src
    

    这是一个适用于 Safari 的 JSFiddle:

    https://jsfiddle.net/5vfv24wu/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-26
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-10
      • 1970-01-01
      相关资源
      最近更新 更多