最后一个斜杠之后的 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');
});
});
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为您只需要最后一个斜杠之后的部分:
检查以下代码:
As you want just part after last slash:
check the following code:
$("#pic").attr("src").split('/')[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