将样式表插入iframe

Insert stylesheet into iframe

本文关键字:iframe 插入 样式      更新时间:2023-09-26

我打开这个线程是因为我尝试了很多在这里或其他地方找到的方法,用jQuery将我的自定义样式表插入到iframe中,但都不起作用。现在我被卡住了。我愿意接受任何建议,对我来说,如果有任何其他灵魂,就没有必要用jQuery来做。

这就是我现在拥有的:

<!doctype html>
<html lang="hu">
<head>
<meta charset="utf-8">
<title><?php echo $this->title; ?></title>
<link rel="stylesheet" type="text/css" href="css/style.css" media="all" />
<script src="//ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js"></script>
<script src="js/mootools-more.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
  $('#iwiw').load(function(){
    $('#iwiw').contents().find('head').append('<link href="css/iwiw.css" rel="stylesheet" type="text/css" />');
  });
});
</script>
</head>
<body>
<header><!-- content --></header>
<div id="container">
  <div id="left">
    <iframe id="iwiw" src="http://iwiw.hu/like.jsp?u=<?php echo $url; ?>&title=<?php echo str_replace('+', '%20', urlencode($this->title)); ?>&t=tetszik&s=white" width="220px" height="120px" style="border: none;" scrolling="no" frameBorder="0"></iframe>
  </div>
  <div id="right"><!-- content --></div>
</div>
<footer><!-- content --></footer>
</body>

在代码中$url是用urlencode编码的当前url,$this->title是当前页面标题,正如您所看到的,我的文档类型是html5,您没有看到的是iframe的SRC是XHTML过渡。

实际上iframe是原始的工作代码,如果你想测试它,只需将$url替换为你的网站的url,将$this->标题替换为你网站的标题

除非当前页面也来自iwiw.hu,否则无法执行此操作。存在跨域限制,如果您创建的<iframe>的输出在您的域之外,则无法触摸该输出。

这里有一些答案。。。除非iframe域从主页推迟。

如何将CSS应用于iframe?