我正在尝试iFrame
使用Jquery动态地调整大小。
input
用户可以在其中设置所需的宽度,然后为iFrame
元素设置此宽度。
这是我编写的代码:
$( document ).ready(function() {
var width = 300;
// Get the value of the input width box
$( 'form#WidgetConfigurator_width' ).keyup(function() {
alert("should get the innerHTML or text value here");
});
if ( width < 180 ) {
width = 180;
}
if ( width > 500 ) {
width = 500;
}
$('iframe#WidgetsContent').attr('width', width);
});
问题是alert
,即使iFrame
页面首次加载时设置了默认值,也永远不会显示。我正在逐步执行此操作,因此显示警报是了解我的正确方法的第一步。
<form name="WidgetConfigurator" method="post" action="/widgets" id="WidgetConfigurator" _lpchecked="1">
<div id="WidgetConfigurator">
<div>
<label for="WidgetConfigurator_width">Larghezza del widget</label>
<input type="text" id="WidgetConfigurator_width" name="WidgetConfigurator[width]">
</div>
</div>
</form>
这是代码iFrame
:
<iframe id="WidgetContent" src="http://127.0.0.1:8000/widget/1" frameborder="0" height="600"></iframe>
我想实现的是的大小调整iframe
后用户设置的宽度input
(配置时实做同样的事情页面插件)
有什么建议吗?谢谢!
您选择器表单#WidgetConfigurator_width的输入错误,它必须是#WidgetConfigurator_width的表单,我认为您需要这样的代码
$( document ).ready(function() {
// Get the value of the input width box
$( 'form #WidgetConfigurator_width' ).keyup(function() {
var w=parseInt( $(this).val())
if ( w< 180 ) {
w= 180;
}
if ( w> 500 ) {
w= 500;
}
$('iframe#WidgetContent').width(w)
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form name="WidgetConfigurator" method="post" action="/widgets" id="WidgetConfigurator" _lpchecked="1">
<div id="WidgetConfigurator">
<div>
<label for="WidgetConfigurator_width">Larghezza del widget</label>
<input type="text" id="WidgetConfigurator_width" name="WidgetConfigurator[width]">
</div>
</div>
</form>
<iframe id="WidgetContent" src="http://127.0.0.1:8000/widget/1" frameborder="0" height="600"></iframe>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句