【问题标题】:iframe height adjust not workingiframe 高度调整不起作用
【发布时间】:2011-07-13 05:28:06
【问题描述】:

我有一个带有按钮的 iframe,单击该按钮可展开列表。单击按钮后,我希望该 iframe 的高度进行调整。这是我在 iframe 中的代码:

     <script>
$(function() {
    $("#field12593102_1").click(function() {
        window.parent.adjustIFrameHeight();
    });
});
</script>

父框架中的代码如下:

    function adjustIFrameHeight(){
$('.childframe').css('height', function(index) {
  return index +=400;});
};

我知道父框架代码可以工作,因为我在没有调用函数的情况下尝试过它,但它不像现在那样工作。

【问题讨论】:

    标签: javascript


    【解决方案1】:

    您需要使用 css() 调用的函数的第二个参数

    function adjustIFrameHeight(){
    $('.childframe').css('height', function(index,value) {
      return value + 400;});
    };
    

    第一个参数是 jQuery 对象内当前元素的索引,而不是当前属性(在本例中为宽度)。

    你也可以使用:

    function adjustIFrameHeight(){
        $('.childframe').css('height', '+=400');
        };
    

    【讨论】:

    • 这在iframe和父文档放在不同域时不起作用,它受same-origin-policy限制
    • @zach:这是一个跨域 iframe。由于同源策略,您不能跨域编写脚本。
    • 哦,我明白了。感谢您的帮助!
    【解决方案2】:

    改变:

    function adjustIFrameHeight(){
    $('.childframe').css('height', function(index) {
      return index +=400;});
    };
    

    到:

    function adjustIFrameHeight(){
        $('.childframe').css({height: function(index, value) {
          return value +=400;}
        });
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      相关资源
      最近更新 更多