我希望根据div的背景图像加载时运行一个函数。我错过了什么?
JQUERY
$(function () {
$('.productnew .imgframe').each(function () {
$(this).load(function () {
$(this).attr("style", $(this).attr("style").replace("background:", ""));
$(this).attr('style', 'background:url(images/new.png), ' + $(this).attr('style'));
});
});
});
HTML
<div class="poster productnew"><div class="imgframe" style="background:url(images/posters/bicycle.jpg);"></div>
答案 0 :(得分:1)
div元素(你的<div class="imgframe">
元素)没有onload事件,这就是你没有得到事件的原因。
有一些onload事件的元素是<image>
,<body>
,<iframe>
。
如果您需要知道何时加载该特定图像以触发其他代码,您可以使用javascript手动将图像加载到图像对象中,然后当该onload事件触发时,您将知道图像已加载并且您可以触发其他事件(将其设置为后台URL或您想要做的任何其他事件)。