【问题标题】:Add styling to iFrame with jQuery使用 jQuery 向 iFrame 添加样式
【发布时间】:2018-12-08 00:28:06
【问题描述】:

我们正在将保险报价表(报价即服务)作为 iFrame 嵌入到我们的网页中。它确实不适用于我们的设计,因此我们正在尝试使用 jQuery 更新 CSS。

<iframe src="https://www.agentinsure.com/compare/auto-insurance-home-insurance/insura/quote.aspx" width="100%" height="2000" frameborder="0"></iframe>

jQuery(document).ready(function($) {
  $('iframe').on('load', function() {
    let el = $(this).contents().find('body');
    el.css('background', 'red');
    el.html('<p>jquery added this content</p>');
  });
});

这是一个指向具有相同代码的 codepen 的链接。 https://codepen.io/ben_jammin/pen/KbPRxW

在我们实际的 Wordpress 网站中使用相同的 jQuery 代码,我们会收到另一个错误 Uncaught DOMException: Blocked a frame with origin

因此,如果可能,请寻求有关如何更新 iFrame 中的 CSS 的建议。

https://insura4you.kavoddigital.com/test-2

jquery.js?ver=1.12.4:2 Uncaught DOMException: Blocked a frame with origin "https://insura4you.kavoddigital.com" from accessing a cross-origin frame.
    at contents (https://insura4you.kavoddigital.com/wp-includes/js/jquery/jquery.js?ver=1.12.4:2:26921)
    at Function.map (https://insura4you.kavoddigital.com/wp-includes/js/jquery/jquery.js?ver=1.12.4:2:3613)
    at a.fn.init.n.fn.(anonymous function) [as contents] (https://insura4you.kavoddigital.com/wp-includes/js/jquery/jquery.js?ver=1.12.4:2:27001)
    at HTMLIFrameElement.<anonymous> (https://insura4you.kavoddigital.com/wp-content/themes/insurel/js/theme.init.js:221:13)
    at HTMLIFrameElement.dispatch (https://insura4you.kavoddigital.com/wp-includes/js/jquery/jquery.js?ver=1.12.4:3:12444)
    at HTMLIFrameElement.r.handle (https://insura4you.kavoddigital.com/wp-includes/js/jquery/jquery.js?ver=1.12.4:3:9173)
contents @ jquery.js?ver=1.12.4:2
map @ jquery.js?ver=1.12.4:2
n.fn.(anonymous function) @ jquery.js?ver=1.12.4:2
(anonymous) @ theme.init.js:221
dispatch @ jquery.js?ver=1.12.4:3
r.handle @ jquery.js?ver=1.12.4:3
load (async)
add @ jquery.js?ver=1.12.4:3
a.event.add @ jquery-migrate.min.js?ver=1.4.1:2
(anonymous) @ jquery.js?ver=1.12.4:3
each @ jquery.js?ver=1.12.4:2
each @ jquery.js?ver=1.12.4:2
sa @ jquery.js?ver=1.12.4:3
on @ jquery.js?ver=1.12.4:3
(anonymous) @ theme.init.js:220
i @ jquery.js?ver=1.12.4:2
fireWith @ jquery.js?ver=1.12.4:2
ready @ jquery.js?ver=1.12.4:2
K @ jquery.js?ver=1.12.4:2

【问题讨论】:

    标签: jquery css iframe


    【解决方案1】:

    这意味着嵌入式网站禁止您访问/修改其内容。没有主流浏览器会让你打破这个设置。无法操作此类 iframe 的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-10
      • 2010-10-12
      • 2013-11-08
      • 1970-01-01
      • 2011-05-25
      • 1970-01-01
      • 2019-11-17
      相关资源
      最近更新 更多