点击iframe改变父元素

代码结构类似于这样


<div class="iframe_div">
    <iframe class="iframe" name="iframe" src="div.html"></iframe>
    <iframe class="iframe none" name="iframe" src="http://www.sina.com"></iframe>
    <iframe class="iframe none" name="iframe" src="http://www.163.com"></iframe>
</div>

我想点击里面的iframe改变父元素“iframe_div”的相关样式,比如高宽什么的要怎么做?
谢谢

阅读 7.8k
4 个回答
$("iframe").click(function(){
    $(".iframe_div").style.height="100px";
    $(".iframe_div").style.width="100px";
)

$("iframe").click(function(){
$(this).parent().css('height', 'xxpx');
$(this).parent().css('width', 'yypx');
});
父元素的话可以用.parent()
如果是其它元素也可以各种用选择器去选。
但我看过一本叫《编写可维护的Javascript》还是什么的书,里面有说js里面最好不要直接操作css,要改变样式可以通过class
就是说把上面的回调函数内容换成
$(this).parent().addClass('xxClass');
$(this).parent().removeClass('yyClass');
之类的
然后样式定义在css里面
.xxClass{
样式
}
反正我挺喜欢这样的也说一下你可以参考参考~( ̄▽ ̄)~

新手上路,请多包涵

在里面的iframe call window.parent

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题