【问题标题】:Why my jquery don't take that classes of this iframe?为什么我的 jquery 不采用这个 iframe 的类?
【发布时间】:2023-04-05 20:13:01
【问题描述】:

我正在尝试获取名称为“titular_portada”的所有课程。

有很多,但由于某种原因不起作用:

<!DOCTYPE html>
<html lang="es">
<head>
    <link rel="stylesheet" href="css/styles.css">
</head>
<body>
    <iframe src="https://vandal.elespanol.com" id="iframe" width="100%" height="100%"></iframe>
    <script src="js/jquery-3.6.0.js"></script>
</body>
</html>
$(window).on("load", function() {
    $("#iframe > .titulo_portada").each((i, el) => {
        console.log(el);
    });
});

【问题讨论】:

  • iframe 是否与主页在同一个域中?
  • 摆脱&gt;。这只查找直系子代,但至少有 &lt;htm&gt;&lt;body&gt; 元素介于两者之间。

标签: html jquery iframe


【解决方案1】:
$(window).on("load", function() {
    var iframe = document.getElementById("iframe");  
    var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
    $(".titulo_portada",$(iframeDocument)).each((i, el) => {
        console.log(el);
    });
});

它应该可以工作,但是 https://jsfiddle.net/NickU/65dxzg3s/9/ 的 jsfiddle 显示错误“Uncaught SecurityError: Blocked a frame with origin "https://fiddle.jshell.net" from access a cross-origin frame.”

【讨论】:

    猜你喜欢
    • 2017-05-23
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 2012-01-07
    相关资源
    最近更新 更多