最后一次斜线后jQuery .split()?

我无法在.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'); }); }); 

正如你想要的最后一个斜杠之后:检查以下代码:

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

$(“#pic”)。attr(“src”)。split(’/’)[1]只给你分裂数组中的第二个元素,而你需要所有分裂元素首先出现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'; });