gpt4 book ai didi

javascript - jQuery/CSS - 单击 iframe 内容内的按钮时更改 IFRAME 高度

转载 作者:行者123 更新时间:2023-11-28 09:06:12 25 4
gpt4 key购买 nike

我正在处理一个简单的 HTML/jQuery 脚本。现在,当我单击 iframe 内的按钮时,它正在更改 iframe 的高度,即使用按钮调用内容。

看一下代码:

<div id="outerdiv" style="padding:20px;border:2px solid red;">
<iframe src="http://beta.sportsdirect.bg/test/iframe.php" id="inneriframe" scrolling="no" target="_parent"></iframe>
</div>

这是 iframe.php 的内容:

 <HTML>
<head>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<div style="display:block;height:300px;">
<h3>The iframe content</h3>
<button type="button" id="SuperWebF1">Click me to resize the holding Iframe!</button>
</div>
<script type="text/javascript">
$("#SuperWebF1").click(function(){
$('#inneriframe', window.parent.document).animate({height:'900px'}, 500);

})
</script>

当我尝试添加这个时,问题来了:

<style>
#outerdiv
{
width:400px;
height:300px;
overflow:hidden;
position:relative;
border:2px solid #fff;
}

#inneriframe
{
position:absolute;
width:400px;
height:700px;
border:0px;
}
</style>

<div id="outerdiv" style="padding:20px;border:2px solid red;">
<iframe src="http://beta.sportsdirect.bg/test/iframe.php" id="inneriframe" scrolling="no" target="_parent"></iframe>
</div>

如你所见,我添加了一个 <style>我为元素添加 CSS 的标记 outerdiv和 iframe inerriframe现在,当我点击按钮时,它不会改变 iframe 的高度。

当我删除 <style>....</style>标签及其中的所有内容脚本再次开始工作。

这是演示:http://beta.sportsdirect.bg/test/这是我没有添加 <style></style> 时的工作演示标签。

你能帮我设置这些元素的 CSS 并使 jQuery 脚本也能正常工作吗?

提前致谢!

最佳答案

iframe 页面中的文档未完全加载时,该事件不会触发它的功能。

代替:

<script type="text/javascript">
$("#SuperWebF1").click(function(){
$('#inneriframe', window.parent.document).animate({height:'900px'}, 500);

});
</script>

使用:

<script type="text/javascript">
$(document).ready(function(){
$("#SuperWebF1").on('click',function(){
$('#inneriframe', window.parent.document).animate({height:'900px'}, 500);

});
});
</script>

当页面中的所有.css.js和整个文档都准备好后,让它监听click事件。

我希望这会有所帮助吗?

关于javascript - jQuery/CSS - 单击 iframe 内容内的按钮时更改 IFRAME 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26685008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com