0
特定のボタンをクリックしたときに画像の保存先フォルダを変更したり、新しい選択したフォルダを保持している間に別の要素をホバリングしたときにこの画像のsrcを変更したりする際に問題があります。jQuery change img and folder 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変更ボタンの上にカーソルを置くと、フォルダは元の変数に行くことですが、それは維持する必要があります選択したフォルダ
これがどのように正しく機能するかについてのアイデアはありますか?
ジュハナありがとうございます。 新しい問題が発生しました。このスクリプトは完全にローカルで働いていましたが、今はサーバー上にいくつかのフォルダを深くインストールしています。とにかく、私の前提では、split()はいくつかのディレクトリを深く掘り下げているので、いくつかの問題を引き起こしています。 .split( '/')[1]は何をするのか分かりませんが、最後のスラッシュの後に分割する方法はありますか? – tjcss
'split( '/')'は、文字をセパレータとして使用して文字列の配列を作成します。 '[1]'はその配列の2番目の項目を選択します(配列は0から始まります)。分割配列の最後の要素を取得する方法については、http://stackoverflow.com/questions/3235043/last-element-of-array-in-javascriptを参照してください。 – JJJ