jquery - 访问存储在 Jquery 变量中的类名

标签 jquery html css

网站上有三张图片。

<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/

相关文章:

html - CSS:在不同的标准模式下检测 IE?

javascript - 将静态内容插入 jQuery UI 菜单小部件

html - 为什么IE11选择渲染模式: "IE7 Strict" and how to i make it use current browser?

jquery - 无法修复的重叠

python - 在 css 中使用 Django 数字变量

html - Bootstrap 密码部分在点击时损坏

javascript - 使用 PHPExcel 从 Excel 加载动态数据

jquery - 如果值为 false 则停止 ajax 调用(在任何 ajax 调用之前)

jquery - 将 Flexbox 与 jQueryUI 的 sortable 结合使用

javascript - 如何修复 Highcharts 错误 #13?