jQuery 更改 img 和文件夹 src

发布于 2024-11-25 14:35:43 字数 1256 浏览 1 评论 0原文

我在单击某个按钮时更改图像的目标文件夹,以及在将鼠标悬停在另一个元素上时更改该图像的 src,同时保留新选择的文件夹时遇到问题。

我的代码在这里,jquery 摘录如下:

<script type="text/javascript">
$(document).ready(function(){
    imgFldr = 'period-black';

    //click the hardware buttons and change the folder where the images are coming from, but not the image itself (by name)
    $('#standardBlack').click(function(){
        $("#pic").attr("src",'standard-black/'+$("#pic").attr("src").split('/')[1]);
    });

    $('#standardGold').click(function(){
        $("#pic").attr("src",'standard-gold/'+$("#pic").attr("src").split('/')[1]);
    });

    $('#standardChrome').click(function(){
        $("#pic").attr("src",'standard-chrome/'+$("#pic").attr("src").split('/')[1]);
    });

    //on hovering the 21 or 24 colour options, change the colour of the image but not the folder
    $('#black').hover(function(){
        $("#pic").attr("src",imgFldr+"/black.jpg");
    });
    $('#blueGrey').hover(function(){
        $("#pic").attr("src",imgFldr+"/blue-grey.jpg");
    });

    $('#burgundy').hover(function(){
        $("#pic").attr("src",imgFldr+"/burgundy.jpg");
    });

});
</script>

发生的情况是,一旦您将鼠标悬停在 src 更改按钮上,文件夹就会返回到原始变量,但它应该保留所选文件夹。

关于如何正常工作有什么想法吗?

I'm having problems with changing the destination folder of my images when clicking a certain button, and changing the src of this image when hovering over another element, whilst retaining the new chosen folder.

The code I have is here, with the jquery excerpt here:

<script type="text/javascript">
$(document).ready(function(){
    imgFldr = 'period-black';

    //click the hardware buttons and change the folder where the images are coming from, but not the image itself (by name)
    $('#standardBlack').click(function(){
        $("#pic").attr("src",'standard-black/'+$("#pic").attr("src").split('/')[1]);
    });

    $('#standardGold').click(function(){
        $("#pic").attr("src",'standard-gold/'+$("#pic").attr("src").split('/')[1]);
    });

    $('#standardChrome').click(function(){
        $("#pic").attr("src",'standard-chrome/'+$("#pic").attr("src").split('/')[1]);
    });

    //on hovering the 21 or 24 colour options, change the colour of the image but not the folder
    $('#black').hover(function(){
        $("#pic").attr("src",imgFldr+"/black.jpg");
    });
    $('#blueGrey').hover(function(){
        $("#pic").attr("src",imgFldr+"/blue-grey.jpg");
    });

    $('#burgundy').hover(function(){
        $("#pic").attr("src",imgFldr+"/burgundy.jpg");
    });

});
</script>

Whats happening is that once you hover over the src change button, the folder goes back to the original variable, but it should keep the chosen folder.

Any ideas on how this could work properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城之時有危險 2024-12-02 14:35:43

如果我正确理解了脚本的作用:

$('#standardBlack').click(function(){
    $("#pic").attr("src",'standard-black/'+$("#pic").attr("src").split('/')[1]);
    imgFldr = 'standard-black';
});

等等。

If I understood correctly what the script does:

$('#standardBlack').click(function(){
    $("#pic").attr("src",'standard-black/'+$("#pic").attr("src").split('/')[1]);
    imgFldr = 'standard-black';
});

and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文