javascript 如何通过 jQuery 在 iframe 中更改/添加 CSS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32477273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change/add CSS inside of iframe by jQuery
提问by vineet
I want to add css properties in a iframe div by using jQuery but it's not working.
我想使用 jQuery 在 iframe div 中添加 css 属性,但它不起作用。
Html Code:-
Html 代码:-
<html>
<body>
<iframe id='iframeId' src='api.php?abc=xyz..'>
<html>
<head></head>
<body>
<div class='change-class'> //require <div class='change-class' style='background-color:black'>
.......
</div>
</body>
</html>
</iframe>
</body>
</html>
JS Code:-
JS代码:-
I try below code but doesn't get any result.
我尝试下面的代码,但没有得到任何结果。
$('.change-class').css('background-color','black');
$("#iframeId").contents().find(".change-class").attr('style','background-color=black');
回答by Jai
For this the iframe src document should also have to be on same domain to access iframe's dom nodes, if you have both pages on same domain then you can do this:
为此,iframe src 文档也必须在同一个域中才能访问 iframe 的 dom 节点,如果两个页面都在同一个域中,则可以执行以下操作:
$("#iframeId").contents().find(".change-class").css('background-color', 'black');
回答by guvenckardas
You can find use like this way:
你可以这样找到用途:
var iframe = $('iframe');
$(".change-class", iframe.contents()).addClass('border');