[英]How to apply CSS to iframe using jQuery?
How do I access the iframe and override the elements style "td.rank" ? 如何访问iframe并覆盖元素样式“ td.rank”? This is what I've got so far:
到目前为止,这是我得到的:
<script>
var head = $("#iframe11").contents().find("head");
var css = '<style type="text/css">' +
'td.rank{background-color: #fc6b0a!important;}; ' +
'</style>';
$(head).append(css);
</script>
<iframe src="http://www.example.com" id="iframe11" style="margin-left: 174px; width: 401px; border: 0px solid black; height: 358px; opacity: 0.85; margin-top: 23px;"></iframe>
When I open the code using "inspect element" - I don't even see the CSS code part in the <head>
tag of the iframe. 当我使用“检查元素”打开代码时-我什至在iframe的
<head>
标记中都看不到CSS代码部分。
load
handler for the frame so that the DOM you want to manipulate exists before you try to manipulate it load
处理程序,以便在尝试操纵DOM之前就存在要操纵的DOM。 You if would like add css style inside iframe using jQuery then just follow below steps... 您是否想使用jQuery在iframe中添加CSS样式,然后按照以下步骤操作...
1. First create 'index.html' file and add below code in it 1.首先创建“ index.html”文件,并在其中添加以下代码
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.6.3.min.js"></script>
<script>
$(document).ready(function() {
$('#iframe').load(function() {
$("#iframe").contents().find("head").append("<style>.text_color{color:red;}@page{margin:0;}</style>");
});
});
</script>
</head>
<body>
<iframe src="iframe.html" id="iframe" name="iframe"></iframe>
</body>
</html>
2. Next create another file called 'iframe.html' and add below code to it 2.接下来,创建另一个名为“ iframe.html”的文件,并向其中添加以下代码
<!DOCTYPE html>
<html>
<body>
<div id="text"><span class="text_color">Content inside iframe goes here<span></div>
</body>
</html>
3. Next run 'index.html' file and now see 'Content inside iframe goes here' text color will changed to red color 3.下一步运行“ index.html”文件,现在看到“ iframe中的内容会移至此处”,文本颜色将变为红色
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.