【问题标题】:jquery offset() return empty string in each() in firefox 12jquery offset() 在 Firefox 12 中的 each() 中返回空字符串
【发布时间】:2012-05-10 17:47:19
【问题描述】:

http://jsfiddle.net/suenot/3b3XM/1/

// in FF and IE each() don't work offset()
// $(this).offset().top return empty string
$(document).ready(function(){
    var index = 0;
    $('.box').each(function(){
        var background = $(this).css('background');
        $(this).css('background', 'none');
        var height = $(this).css('height');
        var top = $(this).offset().top;
        $('body').prepend('<div id="box' + ++index + '"></div>');
        $('#box' + index).css({
            'height': height,
            'background': background,
            'position': 'absolute',
            'z-index': '-1',
            'top': top,
            'width': '100%'
        });
    });
});​

请帮帮我,我找不到解决方案。

【问题讨论】:

  • 刚刚在 FF 12 中测试过,它工作正常 .offset().top 返回一个值。
  • 在 each() 中测试了吗?在jsfiddle中也可以吗?对不起我的英语。
  • 请贴一些代码sn-ps,而不仅仅是一个链接。
  • 问题是$(this).css('background')返回一个空字符串。
  • 正如 Rocket 所指出的 -> 似乎 $(this).css('background') 在 IE 9 和 Chrome 中运行良好,但在 FF 12 中失败,$(this).css('background-color'); 似乎适用于所有三种浏览器。

标签: javascript jquery firefox each offset


【解决方案1】:

我不确定$(this).css('background-color'); 是否是您想要的,但更新后会显示该框。查看fiddle中的输出

$(document).ready(function(){
    var index = 0;
    $('.box').each(function(){
        var background = $(this).css('background-color');
        $(this).css('background', 'none');
        var height = $(this).css('height');
        var top = $(this).offset().top;
        $('body').prepend('<div id="box' + ++index + '"></div>');
        $('#box' + index).css({
            'height': height,
            'background': background,
            'position': 'absolute',
            'z-index': '-1',
            'top': top,
            'width': '100%'
        });
    });
});

【讨论】:

  • +1,这使得 Firefox 12 和 Chrome 19 中的输出相同。
  • @Rocket 哦,我明白了,$(this).css('background') 在 Chrome 中运行良好。
  • @Rocket tbh,这是一件有趣的事情。如果这个问题的格式正确,就值得点赞。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-03
  • 2012-06-05
  • 2013-12-19
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 2013-06-15
相关资源
最近更新 更多