网站上有三张图片。
<img src="images/kep1.jpg" class="kep1">
<img src="images/kep2.jpg" class="kep2">
<img src="images/kep4.jpg" class="kep4">
单击功能可以在页面中间一次放大一页,但我会将必要的数据存储在变量中。
var pos;
var t;
var h;
var pic;
$("img").click(function(){
pos = $(this).css("position");
pic = $(this).attr("class");
t = $(this).css("top");
h = $(this).css("height");
$(".szurkeDiv").show();
$(this).animate(
{
"position" : "fixed",
"top" : "50%",
"left" : "50%",
"margin-top" : "-300px",
"margin-left" : "-400px",
"width" : "800px",
"height" : "600px"
}, 1000, function(){}
);
});
然后我希望您单击页面中间的图像,将其动画返回到原始位置。我想访问 PIC 变量但没有成功。
$(pic).animate(
{
"position" : pos,
"width" : "150px",
"margin-top" : "0px",
"margin-left" : "0px",
"left" : "5px",
"height" : h,
"top" : t
}, 1000, function(){}
);
如何才能很好地做到这一点,引用 PIC 变量?
感谢您的帮助!!
最佳答案
尝试使用 $('.' + pic) 而不是 $(pic)。
参见https://jsfiddle.net/g15v3c4z/
var pic = $('p').attr("class");
$('.' + pic).html('world!');
关于jquery - 访问存储在 Jquery 变量中的类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53266706/