【问题标题】:Get The iframe attribute data from inside of the iframe从 iframe 内部获取 iframe 属性数据
【发布时间】:2021-10-14 12:32:03
【问题描述】:

我想知道是否可以使用 Javascript 从该 iframe 中获取 iframe 属性。

例如,假设我有:

<iframe src"google.com" class="myClass" id="myId"></iframe>

如果我想从 iframe 中获取 class 属性 (MyClass) 的值,我该怎么做?

我试过window.class & window.className

PS:如果在我道歉之前有人问过这个问题,我已经查看过,但找不到可以回答的问题。

【问题讨论】:

  • frame src 是否与页面框架在同一个域/源上?如果是这样,您需要遍历顶部窗口并查看该文档。如果来源不同,则浏览器安全策略将阻止您
  • 在上面的示例中,您正在运行来自 google.com 的代码。或者您问如何从 iframe 旁边的脚本中的 js 获取 myclass

标签: javascript html typescript iframe


【解决方案1】:

您无法使用 window.className 获取属性的值。

改用这个...

const iframe = document.getElementById("myId");

iframe.getAttribute("class"); //<-you can select any attribute with this method
    //or
console.log(iframe.classList)

【讨论】:

    猜你喜欢
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多