2011-07-20 104 views
0

我一直没能在.split()上找到很多文档,并且我有一些代码可以在单击两个单独按钮时更改图像的来源以及图像的目标文件夹。一个按钮用于更改源文件夹,另一个用于更改实际的jpeg名称。jQuery .split()在最后一个斜杠后?

我有脚本工作正常,但现在它是实时的,它是多个文件夹深,当我点击更改文件夹,默认图像隐藏/不显示实际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'); 
    }); 
}); 
+0

您的代码看起来不错。你可以发布标记的一部分吗? – ShankarSangoli

+0

是的,这里你去:http://pastebin.me/74fc57b4f094813151564c8ca00a558c注意:这是一个wordpress模板文件。 – tjcss

+3

良好做法 - 多次使用局部变量而不是调用“#pic”。 –

回答

0

$( “#PIC”)ATTR( “SRC”)分割( '/')[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'; 
    }); 
+0

嗨ShankarSangoli,这给了我一个像这样的img源代码: tjcss

+0

当你连接文件夹和图像源的一部分时,不要放“/”。看看这个[imgSrc ='standard-black'+ imgSrc.substring(imgSrc.indexOf('/'));]'standar-black'中没有“/”。 – ShankarSangoli

+0

我不确定我是否理解你,我完全使用过你的代码示例,我做错了什么? – tjcss

1

: 检查下面的代码:

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

拆分和替换的文档是regexp的文档 –

+0

Crikey,谢谢eicto,但我根本不懂正则表达式! (假设这是一个正则表达式?) 如何使用此代码使用我之前的示例和我的代码库代码? – tjcss

+0

'function lastslash(t) { return t.replace(/^.+\/([^\/]*)$/,'$ 1'); }' –

相关问题