最后一个斜杠之后的 jQuery .split() ?

发布于 2024-11-25 04:11:28 字数 1176 浏览 0 评论 0原文

我无法找到有关 .split() 的太多文档,并且我有一些代码可以通过单击两个单独的按钮来更改图像的源以及图像的目标文件夹。一个按钮更改源文件夹,另一个按钮更改实际的 jpeg 名称。

我的脚本工作正常,但现在它已经上线,并且有多个文件夹,当我单击更改文件夹时,默认图像在最后一个/之后隐藏/不显示实际的 jpg,直到我单击另一个按钮。我的jquery如下:

$(document).ready(function(){
    siteUrl = 'http://webdev.timberwindows.com/wp-content/themes/TimberWindows/images/window-planner/';
    imgFldr = 'period-black';

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

    //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]);
        imgFldr = 'standard-black';
    });
        $("#ironmongery li").click(function(){
        $('#ironmongery>li').removeClass('selected');
        $(this).addClass('selected');
    });
        $("#colours li").click(function(){
        $('#colours>li').removeClass('selected');
        $(this).addClass('selected');
    });
});

I haven't been able to find much documentation on .split(), and I have some code that changes the source of an image and also the target folder of the image on clicking two seperate buttons. One button changes the source folder, the other the actual jpeg name.

I had the script working fine, but now it's on live and it's multiple folders deep and when i click to change the folder, the default image hides / doesn't display the actual jpg after the last / until i click the other button. the jquery i have is as follows:

$(document).ready(function(){
    siteUrl = 'http://webdev.timberwindows.com/wp-content/themes/TimberWindows/images/window-planner/';
    imgFldr = 'period-black';

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

    //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]);
        imgFldr = 'standard-black';
    });
        $("#ironmongery li").click(function(){
        $('#ironmongery>li').removeClass('selected');
        $(this).addClass('selected');
    });
        $("#colours li").click(function(){
        $('#colours>li').removeClass('selected');
        $(this).addClass('selected');
    });
});

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

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

发布评论

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

评论(2

这样的小城市 2024-12-02 04:11:28

因为您只需要最后一个斜杠之后的部分:
检查以下代码:

var t="http://test.test.com/test/test.php";
console.log(t.replace(/^.+\/([^\/]*)$/,'$1'));

As you want just part after last slash:
check the following code:

var t="http://test.test.com/test/test.php";
console.log(t.replace(/^.+\/([^\/]*)$/,'$1'));
晨曦慕雪 2024-12-02 04:11:28

$("#pic").attr("src").split('/')[1] 只会为您提供分割数组中的第二个元素,而您需要除第一个元素之外的所有分割元素。请尝试此操作

    $('#standardBlack').click(function(){
        var imgSrc = $("#pic").attr("src");
        imgSrc = 'standard-black' + imgSrc.substring(imgSrc.indexOf('/'));
        $("#pic").attr("src", imgSrc);
        imgFldr = 'standard-black';
    });

    $('#standardChrome').click(function(){
        var imgSrc = $("#pic").attr("src");
        imgSrc = 'standard-chrome' + imgSrc.substring(imgSrc.indexOf('/'));
        $("#pic").attr("src", imgSrc);
        imgFldr = 'standard-chrome';
    });

$("#pic").attr("src").split('/')[1] will give you only the second element from splitted array whereas you need the all the splitted elements expect first 1. Please try this

    $('#standardBlack').click(function(){
        var imgSrc = $("#pic").attr("src");
        imgSrc = 'standard-black' + imgSrc.substring(imgSrc.indexOf('/'));
        $("#pic").attr("src", imgSrc);
        imgFldr = 'standard-black';
    });

    $('#standardChrome').click(function(){
        var imgSrc = $("#pic").attr("src");
        imgSrc = 'standard-chrome' + imgSrc.substring(imgSrc.indexOf('/'));
        $("#pic").attr("src", imgSrc);
        imgFldr = 'standard-chrome';
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文