2011-07-20 71 views
0

我在点击某个按钮时更改图像的目标文件夹时发生问题,并在保留新选定的文件夹时将此图像的src更改为鼠标悬停在另一个元素上。jQuery更改img和文件夹src

我的代码是在这里,与jQuery的摘录在这里:

<script type="text/javascript"> 
$(document).ready(function(){ 
    imgFldr = 'period-black'; 

    //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]); 
    }); 

    $('#standardGold').click(function(){ 
     $("#pic").attr("src",'standard-gold/'+$("#pic").attr("src").split('/')[1]); 
    }); 

    $('#standardChrome').click(function(){ 
     $("#pic").attr("src",'standard-chrome/'+$("#pic").attr("src").split('/')[1]); 
    }); 

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

    $('#burgundy').hover(function(){ 
     $("#pic").attr("src",imgFldr+"/burgundy.jpg"); 
    }); 

}); 
</script> 

请告诉我发生的事情是,一旦你将鼠标悬停在SRC切换按钮,将文件夹又回到了原来的变量,但它应该保持所选文件夹。

关于如何正常工作的任何想法?

回答

0

如果我理解正确的是什么脚本的作用:

$('#standardBlack').click(function(){ 
    $("#pic").attr("src",'standard-black/'+$("#pic").attr("src").split('/')[1]); 
    imgFldr = 'standard-black'; 
}); 

等。

+0

谢谢Juhana。 我现在遇到了一个新问题。这个脚本在本地完美运行,但现在我在服务器上安装了几个文件夹,我认为 - 无论如何,我认为split()会导致一些问题,因为我深入了几个目录。 我不明白.split('/')[1]会做什么,但有没有办法让它在最后一个斜杠后分裂? – tjcss

+0

'split('/')'使用/字符作为分隔符创建一个字符串数组。 '[1]'选择该数组的第二项(当数组从0开始)。查看http://stackoverflow.com/questions/3235043/last-element-of-array-in-javascript获取拆分数组的最后一个元素的方法。 – JJJ