使用 .attr() 设置多个属性
我这里有一个函数:
编辑:在这里给了你完整的代码!
$(".thumbnail").live("click",
function(){
$('#imageBig').find("#pictureContainer").remove();
$('#loadingImage').fadeIn();
var path = $(this).children().attr("src");
var newPath = path.substring(0, path.length-9);
var newPath2 = newPath += ".jpg";
var imageLoad = new Image();
$(imageLoad).load(function () {
if (--imageLoad == 0) {
// ALL DONE!
}
// anything in this function will execute after the image loads
$('#loadingImage').fadeOut();
var newImg = $('<img />').attr('src',$(this).attr('src'));
newImg.css("height","400px").css("width","600px");
$('#imageBig').append( $(newImg) );
})
.attr({
src:newPath2,
id:"pictureContainer"
});
})
我的问题是 .attr()
加载图像时,它确实更改了 src (因此第 1 行“src:newPath2
”有效),但它没有放置 id。因此,当创建我的图像时,它只会执行:。没有身份证。我做错了什么吗?我检查了 jquery 文档几次,我很确定这就是如何做到这一点。
我认为问题出在 .load() 上,但我不确定什么不起作用。
谢谢你!
(如果您需要它,我可以在此之前给您代码)
已解决:
只需将 id 提供给 newImg 而不是 imageLoad 即可。
I'm having a function here:
EDIT : gave you full code here!
$(".thumbnail").live("click",
function(){
$('#imageBig').find("#pictureContainer").remove();
$('#loadingImage').fadeIn();
var path = $(this).children().attr("src");
var newPath = path.substring(0, path.length-9);
var newPath2 = newPath += ".jpg";
var imageLoad = new Image();
$(imageLoad).load(function () {
if (--imageLoad == 0) {
// ALL DONE!
}
// anything in this function will execute after the image loads
$('#loadingImage').fadeOut();
var newImg = $('<img />').attr('src',$(this).attr('src'));
newImg.css("height","400px").css("width","600px");
$('#imageBig').append( $(newImg) );
})
.attr({
src:newPath2,
id:"pictureContainer"
});
})
My problem is with the .attr()
When loading the image, it does change the src (so line 1 "src:newPath2
" works), but it doesn't put the id. So when my image is created, it only does : <img src="newpath2" />
. No id. Am I doing something wrong? I checked the jquery documentation a few times and i'm pretty sure that's how to do it.
I think the problem comes with the .load() but i'm not sure what doesn't work.
Thank you!
(if you need it, I can give you the code before that)
SOLVED :
Just have to give the id to newImg instead of imageLoad.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于
$('').attr('src',$(this).attr('src'))
,newImg 具有相同的 SRC,但 ID 不同,那么我认为您正在附加(并引用)newImg,因此您应该将 ID 提供给 newImg 而不是 loadImage。newImg has the same SRC because of
$('<img />').attr('src',$(this).attr('src'))
, but not the same ID, then I think you are appending (and referring to) newImg, so you should give the ID to newImg instead of loadImage.二传手和吸气手!
使用:
Setter and Getter!
Use:
我可能对你想要做的事情有点困惑,但为什么不直接做:
那不会也给你id吗?
I might be a bit confused by what you are trying to do but why not just do:
Wouldn't that give you the id as well?