我有这个脚本可以根据为其设置的背景图像的高度调整 div 的高度。
$(document).ready(function() {
var url = $('#header').css('background-image').replace('url(', '').replace(')', '').replace("'", '').replace('"', '');
var bgImg = $('<img />');
bgImg.hide();
bgImg.bind('load', function()
{
var height = $(this).height();
$("#content").css("margin-top",height+"px");
$("#header").css("min-height",height+"px");
});
$('#header').append(bgImg);
bgImg.attr('src', url);
});
出于某种原因,它在 Chrome 和 Safari 中都可以正常工作,但在 Firefox 中却不行。我已经检查了 FF 中的控制台,看是否有任何错误,但没有。不确定这里是否有与 FF 不兼容的东西或一些我不熟悉的奇怪的 FF 东西。任何评论表示赞赏。
最佳答案
问题是 .replace("'", '')
和 .replace('"', '')
只替换第一个引号。
然后,您可以对 g
使用正则表达式(全局)标志:.replace(/'/g, '')
和 .replace(/"/g, '')
.或者更好的是,加入他们:
.replace(/url\(|\)|'|"/g, '');
关于javascript - 根据背景图像大小调整 div 大小的脚本——在 Firefox 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19073420/