在 IE 7 和 IE 8 中加载图像时无法设置标题属性
我正在调试我编写的背景图像幻灯片。
该脚本的流程是加载第一个图像并显示它,然后调用 preloadImages 函数,该函数然后以预设的时间间隔加载其余图像。
preload_img
是在我的插件开始时定义的全局变量 ( var preload_img = 1;
),它跟踪当前预加载的图像编号并保持其应有的值,但该值不会应用于图像的标题属性。
// load the images in sequence one after another
function preloadImages()
{
clearInterval(interval_preload);
var div = '<div id="'+options.id_holder+preload_img+'" class="bg_img_holder"></div>';
$('#'+options.id_wrapper).append(div);
var img = new Image();
//alert("loading: " + options.img_path + images[preload_img].image + " " + preload_img);
$(img).load(function()
{
alert("loaded: " + $(this).attr('src') + " " + $(this).attr('title'));
$img_holder = $('#'+options.id_holder+$(this).attr('title'));
$img_holder.append(this)
.css({
'display': 'none',
'background-color': images[$(this).attr('title')].background,
'z-index': -500
})
.find('img')
.height($img_holder.width() * 0.5625);
}).attr({
'src': options.img_path + images[preload_img].image,
'title': preload_img
});
//if there's another image to load after this one
if(++preload_img < (images.length))
{
interval_preload = setInterval(preloadImages,1500);
}
}
尽管 preload_img
是在加载图像时定义的(第一个被注释掉的警报显示正确的值),但当图像加载时,标题是一个空字符串(第二个未注释的警报)。
当页面最初打开时(打开浏览器,输入地址,按 Enter 键),该问题不会出现,但每次都会出现。
这可能是我错过的一些小事,但我花了最后一个小时仔细研究这个,我快要疯了。
有一个非常基本的测试用例: http://jsfiddle.net/72Sy6/4/ 除了 IE7 之外,警报在我测试过的所有浏览器中生成预期数据,IE7 在打开页面时具有预期数据,但在所有其他时间都没有。
我提前对警报框表示歉意,但这是我知道在 IE7 中进行调试的唯一方法:X
编辑:在 IE 8 中的幻灯片中没有遇到错误,但此测试用例显示局部变量同样未定义。
I'm in the process of debugging a background image slideshow I wrote.
The flow of the script is that it loads the first image and displays it and then calls the preloadImages function which then loads the rest of the images at a preset interval.
preload_img
is a global variable defined at the start of my plugin ( var preload_img = 1;
) that keeps track of the current image number being preloaded and keeps its value like it should, but this value isn't being applied to the title attribute of the image.
// load the images in sequence one after another
function preloadImages()
{
clearInterval(interval_preload);
var div = '<div id="'+options.id_holder+preload_img+'" class="bg_img_holder"></div>';
$('#'+options.id_wrapper).append(div);
var img = new Image();
//alert("loading: " + options.img_path + images[preload_img].image + " " + preload_img);
$(img).load(function()
{
alert("loaded: " + $(this).attr('src') + " " + $(this).attr('title'));
$img_holder = $('#'+options.id_holder+$(this).attr('title'));
$img_holder.append(this)
.css({
'display': 'none',
'background-color': images[$(this).attr('title')].background,
'z-index': -500
})
.find('img')
.height($img_holder.width() * 0.5625);
}).attr({
'src': options.img_path + images[preload_img].image,
'title': preload_img
});
//if there's another image to load after this one
if(++preload_img < (images.length))
{
interval_preload = setInterval(preloadImages,1500);
}
}
Although preload_img
is defined while it loads the image ( the first alert which is commented out shows the correct value ) when the image is loaded the title is an empty string (the second, uncommented alert).
The issue doesn't present itself when the page is initially opened (open up browser, type in address, hit enter) but does every other time.
It's probably something small I've missed, but I've spent the last hour looking through this and am going crazy.
There's a really basic test case at: http://jsfiddle.net/72Sy6/4/
The alerts generate the expected data in all the browsers I've tested apart from IE7 which, has the expected data when the page is opened, but all other times does not.
I apologise in advance for the alert boxes, but that's the only way I know of debugging in IE7 :X
edit: Hadn't been experiencing errors in IE 8 with the slideshow, but this testcase shows the local variables as being similarly undefined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于我不完全确定的原因,将其更改
为:
解决了问题。
For a reason I'm not entirely sure of, changing this:
to this:
fixed the problem.