jQuery .split() after last slash?
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');
});
});
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'));
$("#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';
});
精彩评论