HTML iframes可以继承css和javascript数据 [英] Can HTML iframes inherit css and javascript data

查看:569
本文介绍了HTML iframes可以继承css和javascript数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道HTML iframes是否可以从其父级继承CSS数据:

I was wondering if HTML iframes could inherit CSS data from their parent:

Main.html

Main.html

<html>
  <head>
   <style>
     .highlight{
       background: #008fcc;
      }
   </style>
  </head>
 <body>
   <iframe src='frame.html'></iframe>
 </body>
</html>

frame.html

frame.html

hello <font class='highlight'>


推荐答案

iframe 一般不能从父页面继承/访问样式/脚本。

Short answer: No, iframes generically can't inherit/access styles/scripts from their parent page.

长答案:<$ c $> iframe 不能更改样式中的任何内容或执行脚本来自另一个域(您甚至不能通过JavaScript读取其DOM内容)。

Long answer: You can't change anything in the styles or execute scripts inside an iframe if it's coming from another domain (you can't even read its DOM contents via JavaScript).

另一方面,如果它来自与父页面相同的域,您可以通过在父页面中执行与此类似的操作来更改元素的样式或执行脚本(此示例使用jQuery,但可以使用纯JavaScript编写):

On the other hand, if it's coming from the same domain as your parent page, you can change styles of elements or execute scripts, by doing something similar to this in your parent page (this example uses jQuery, but it can be written in plain JavaScript):

$("#iframe_id").contents().find("#some_div").css({color: "rgb(0, 162, 232)"}); //changed style of a div inside the iframe

要执行 iframe

$("#iframe_id").contents().document.functionName(functionParams);

希望有帮助!

这篇关于HTML iframes可以继承css和javascript数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆