1

このページに別のカラーオプションを追加するように依頼されました。マルセイユの標識をクリックすると、トラックがオレンジ色から青色に変わります。もう一度クリックするとオレンジ色に戻ります。右側の探索サインをクリックすると、画像の色が入れ替わった2つのビューが利用可能になります。

ページに黄色になる新しいdivを追加したいと思います。したがって、青いボタンは黄色またはオレンジから青に変わり、青のときにクリックするとオレンジに戻ります。黄色のボタンは、青またはオレンジから黄色に変わり、その後オレンジに戻るはずです。
青いボタンでトラックが黄色になったり、黄色のボタンでトラックが青くなったりしないようにする必要があります。

誰かが[探索]をクリックした場合、選択した色を維持する必要があります。

画像を交換する元のjQueryは次のとおりです。

        var isOrange = true;
        $("#marseilleBtn").click(function(){
         $("img.img-swap").each(function(){
                    if(isOrange){
                        this.src = this.src.replace("_orange","_blue");
                    } else{
                        this.src = this.src.replace("_blue","_orange");
                    }
         });
         if(isOrange){
             isOrange = false;
         } else {
             isOrange = true;
         }
    });

私はこのコードを作成していません。jQueryは初めてなので、これは私にとってやりがいのあることです。私はこのコードを書きました、そしてそれはほとんど機能します。問題は、黄色のトラックがオンになっていて、青い看板をクリックしても色が変わらないことです。また、青いトラックがオンになっているときに黄色の標識をクリックしても、色は変わりません。オレンジから始めた場合にのみ色が変わります...それが理にかなっていることを願っています。

        var isOrange = true;
        $("#marseilleBtn").click(function(){
         $("img.img-swap").each(function(){
                    if(isOrange){
                        this.src = this.src.replace(("_orange") || ("_soleil"),"_blue");
                    } else{
                        this.src = this.src.replace("_blue","_orange");
                    }
         });
         if(isOrange){
             isOrange = false;
         } else {
             isOrange = true;
         }
    });

        var isOrange = true;
        $("#soleilBtn").click(function(){
         $("img.img-swap").each(function(){
                    if(isOrange){
                        this.src = this.src.replace(("_orange") || ("_blue"),"_soleil");
                    } else{
                        this.src = this.src.replace("_soleil","_orange");
                    }
         });
         if(isOrange){
             isOrange = false;
         } else {
             isOrange = true;
         }
    });

誰かが私がやろうとしていることを達成するのを手伝ってくれる?あなたがもっと良いことを知っているなら、私はまったく異なるアプローチを受け入れます...私が与えられたコードから始めていたので、このルートを選びました。ORステートメントも作成しようとしましたが、これはまったく機能しません。理由はわかりません。

    this.src = this.src.replace(('_orange, _yellow'),'_blue');

ボーナス質問!変数isOrangeをtrueとして宣言するこのコードの最初の部分と、それをfalseと宣言する最後のif/elseステートメントに混乱しています。誰かが私のためにそれを分解できますか?私はそれの画像交換部分を理解しています...

         if(isOrange){
             isOrange = false;
         } else {
             isOrange = true;
         }
    });

助けてくれてありがとう。

アップデート:

これに取り組んでいますが、2回変更した後、色を変更するには追加のクリックが必要です。つまり、マルセイユをクリックして、青に変わります。Marseilleをクリックすると、オレンジ色に変わります。クリックしてマルセイユに戻ります。

        var color = "orange";  //first default color of img is orange
          $("#marseilleBtn").click(function(){  //on click 
             $("img.img-swap").each(function(){   // find all the images that has class img-swap ..i guess there is 3 more images there
                  if(color == "orange"){  //check if orange .. which is correct and 
                     this.src = this.src.replace("_orange","_blue"); //change it to blue
                  } else if(color == "yellow"){   //if yellow
                     this.src = this.src.replace("_soleil","_blue"); //chnage to yello
                  }else{ //if blue
                     this.src = this.src.replace("_blue","_orange"); //chnage to yellow
                  }

             });

             if(color =="orange"){
                 color = "blue";
             }else if(color == "blue"){
                 color = "yellow";
             }else{
                 color = "orange";
             }

        });

        var color = "orange";  //first default color of img is orange
          $("#soleilBtn").click(function(){  //on click 
             $("img.img-swap").each(function(){   // find all the images that has class img-swap ..i guess there is 3 more images there
                  if(color == "orange"){  //check if orange .. which is correct and 
                     this.src = this.src.replace("_orange","_soleil"); //change it to yellow
                  } else if(color == "blue"){   //if blue
                     this.src = this.src.replace("_blue","_soleil"); //chnage to yello
                  }else{ //if yellow 
                     this.src = this.src.replace("_soleil","_orange"); //chnage to yellow
                  }

             });

             if(color =="orange"){
                 color = "blue";
             }else if(color == "blue"){
                 color = "yellow";
             }else{
                 color = "orange";
             }

        });
4

2 に答える 2

0

さて、私に最初に飛びつくのは、あなたが同じ機能を2回振るうことです。

あなたもこのようにそれを書くことができます:

var isOrange = true;
$("#marseilleBtn, #soleilBtn").click(function () {
    $("img.img-swap").each(function () {
        if (isOrange) {
            this.src = this.src.replace(("_orange") || ("_soleil"), "_blue");
        } else {
            this.src = this.src.replace("_blue", "_orange");
        }
    });
    if (isOrange) {
        isOrange = false;
    } else {
        isOrange = true;
    }
});

if(isOrange)else isOrange = false;教えて、何色に切り替えますか。したがって、3番目の色を追加する場合は、2つの値しかないため、単純なブール値を使用できなくなります。コードを明確にするために、文字列を使用します。

コードは次のようになります。

var curentColor = "orange";
$("#marseilleBtn, #soleilBtn").click(function () {
    $("img.img-swap").each(function () {
        if (curentColor === "orange") {
            this.src = this.src.replace("_orange", "_blue");
            curentColor = "blue";
        } else if(curentColor === "blue") {
            this.src = this.src.replace("_blue", "_yellow");
            curentColor = "yellow";
        } else if(curentColor === "yellow") {
            this.src = this.src.replace("_yellow", "_orange");
            curentColor = "orange";
        }
    });
});

これにより、色がオレンジから青、黄色に変わり、再びオレンジに戻り、カラーサークルが完成します。

于 2013-03-19T13:21:36.573 に答える
0

これを試して

更新しました

  var color = "orange";  //first default color of img is orange
      $("#marseilleBtn").click(function(){  //on click 
         $("img.img-swap").each(function(){   // find all the images that has class img-swap ..i guess there is 3 more images there
              if(color == "orange"){  //check if orange .. which is correct and 
                 this.src = this.src.replace("_orange","_blue"); //change it to blue
              } else if(color == "yellow"){   //if yellow
                 this.src = this.src.replace("_soleil","_blue"); //chnage to yello
              }else{ //if blue
                 this.src = this.src.replace("_blue","_orange"); //chnage to yellow
              }

         });

         if(color =="orange"){
             color = "blue";
         }else if(color == "yellow"){
             color = "blue";
         }else{
             color = "orange";
         }

    });


      $("#soleilBtn").click(function(){  //on click 
         $("img.img-swap").each(function(){   // find all the images that has class img-swap ..i guess there is 3 more images there
              if(color == "orange"){  //check if orange .. which is correct and 
                 this.src = this.src.replace("_orange","_soleil"); //change it to yellow
              } else if(color == "blue"){   //if blue
                 this.src = this.src.replace("_blue","_soleil"); //chnage to yello
              }else{ //if yellow 
                 this.src = this.src.replace("_soleil","_orange"); //chnage to yellow
              }

         });

         if(color =="orange"){
             color = "yellow";
         }else if(color == "blue"){
             color = "yellow";
         }else{
             color = "orange";
         }

    });

とボーナスの答え!!! 最初var colorはオレンジ色の赤みがかった色です...最後のif/else条件は、正弦波が3つの画像を持っているためExploreです..ループが必要で、ループ内で色をチェックしています...最初の呼び出しで..オレンジは色なので、青に変化し(ループを3回実行します..)、2回目のクリックで値の色を青に変更するif/else状態になります。色が青になっているので..ループが黄色に変わります......など。

于 2013-03-19T13:17:36.673 に答える