【问题标题】:add pixels to dynamic iframe height将像素添加到动态 iframe 高度
【发布时间】:2013-03-25 03:40:10
【问题描述】:

我正在尝试将固定数量的像素添加到动态 iframe 高度。如何将静态像素量添加到 iframe 高度?我正在使用这个 javascript 根据其内容调整 iframe 高度:

    $(document).ready(function()
{
    // Set specific variable to represent all iframe tags.
    var iFrames = document.getElementsByTagName('iframe');

    // Resize heights.
    function iResize()
    {
        // Iterate through all iframes in the page.
        for (var i = 0, j = iFrames.length; i < j; i++)
        {
            // Set inline style to equal the body height of the iframed content.
            iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';
        }
    }

    // Check if browser is Safari or Opera.
    if ($.browser.safari || $.browser.opera)
    {
        // Start timer when loaded.
        $('iframe').load(function()
            {
                setTimeout(iResize, 0);
            }
        );

        // Safari and Opera need a kick-start.
        for (var i = 0, j = iFrames.length; i < j; i++)
        {
            var iSource = iFrames[i].src;
            iFrames[i].src = '';
            iFrames[i].src = iSource;
        }
    }
    else
    {
        // For other good browsers.
        $('iframe').load(function()
            {
                // Set inline style to equal the body height of the iframed content.
                this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
            }
        );

    }
}

);

例如,当我在“px”之前添加数字“100”时,它只会在当前 iframe 高度旁边放置 100。所以如果我的 iframe 高度是 200,如果我使用这个高度将是 200100:

iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + '100px';

【问题讨论】:

    标签: javascript iframe height


    【解决方案1】:

    例如,当我在“px”之前添加数字“100”时,它只会在当前 iframe 高度旁边放置 100。所以如果我的 iframe 高度是 200,那么高度就是 200100

    由于+ 是字符串连接的运算符以及普通的“加号”运算符,因此当您已经在字符串上下文中时会发生这种情况。

    尝试在iFrames[i].contentWindow.document.body.offsetHeight 值上使用parseInt 将其转换为数字,以便以下+ 添加 100 而不是连接它:

    iFrames[i].style.height =
      ( parseInt(iFrames[i].contentWindow.document.body.offsetHeight) + 100 ) + 'px';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-01
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      • 1970-01-01
      相关资源
      最近更新 更多