javascript - 点击iframe改变父元素
PHPz
PHPz 2017-04-10 14:32:39
[JavaScript讨论组]

代码结构类似于这样


<p class="iframe_p">
    <iframe class="iframe" name="iframe" src="p.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>
</p>

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

PHPz
PHPz

学习是最好的投资!

全部回复(4)
ringa_lee
$("iframe").click(function(){
    $(".iframe_p").style.height="100px";
    $(".iframe_p").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

PHP中文网

1楼正解

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号