【发布时间】:2014-07-24 04:23:55
【问题描述】:
我正在为组织制作一个 github gist 画廊,您可以在 elsherbini.github.io/gist-gallery 看到它。
缩略图通过javascript放置在页面上,背景图片设置为内联样式规则。在 Firefox 和 Internet Explorer 中显示背景图像,而在 Chrome 和 Safari 中则不显示。
在Firefox中,以下代码返回背景图片的正确url,而在chrome中返回"none":
$($(".thumbnail")[0]).css("background-image")
因此我无法使用the following fix by andrew cantino:
function refreshBackgrounds(selector) {
// Chrome shim to fix http://groups.google.com/a/chromium.org/group/chromium-bugs/browse_thread/thread/1b6a86d6d4cb8b04/739e937fa945a921
// Remove this once Chrome fixes its bug.
if (/chrome/.test(navigator.userAgent.toLowerCase())) {
$(selector).each(function() {
var $this = $(this);
if ($this.css("background-image")) {
var oldBackgroundImage = $this.css("background-image");
setTimeout(function() {
$this.css("background-image", oldBackgroundImage);
}, 1);
}
});
}
}
当其他人出现类似问题时,我已经尝试在 URL 中添加单引号,正如一些人所建议的那样。如果我查看网络选项卡,它甚至不会请求图像。如果我在 chrome devtools 中手动将背景图像属性设置为新规则,它可以工作,所以我知道这不是 url 格式或愚蠢的语法错误的问题。
chrome 中的这个错误有解决方法吗?
【问题讨论】:
-
我可能错了,但我认为
$this是$(this)的内存副本,不会影响DOM。尝试在代码中将$this替换为$(this)? -
$this仍然是指 DOM 节点,它只是避免让 jQuery 每次查找它。我尝试更改它,但没有解决问题。 -
哦,值得一试,我猜 XD
标签: javascript jquery html css google-chrome