2016-11-22 39 views
-5

私は2つのボタンを持っています。クリックすると、ページが更新されない限り、灰色から黒色に変わり、黒色になります。または他のボタンをクリックします。他のボタンがクリックされた場合、私はそれを黒に変え、最初のボタンをグレーに戻したいと思います。私はJSがこれのための最善の方法だと思っていますが、どうやってそれを行うのか分かりません。クリック(Javascript)時にボタンの色を変更するには?

HTML::

<a id="button"></a> 
<a id="button"></a> 

CSS:事前に

#button { 
display:inline-block; 
    height:10px; 
    width:10px; 
    border-radius:10px; 
    background-color:gray; 
} 

多くのおかげでここ

は、以下の私のコードの一部です。

+0

複数の要素に同じIDを割り当てないでください。これはクラスのためのものです。 – Jecoms

+2

あなたのページでIDは一意でなければなりません – gr3g

+1

何を試しましたか?私はここで何の努力も見ません。 SOはコード作成サービスではありません。その上、このタイプの機能性は何百万回も前からカバーされていました。 – hungerstar

答えて

1

ID名ではなくクラス名に変更し、再利用してはならない、彼らはまだにJavascriptのロジックを適用することが私たちのために、それぞれ固有のID名が必要になりますそれら。

HTML:

<a id="button1" class="button"></a> 
<a id="button2" class="button"></a> 

CSS:

.button 
      { 
       display:inline-block; 
       height:10px; 
       width:10px; 
       border-radius:10px; 
      } 

Javascriptを:

document.getElementById("button1").style.backgroundColor ="gray"; 
document.getElementById("button2").style.backgroundColor ="gray"; 


document.getElementById("button1").onclick = function(){ 
      this.style.backgroundColor ="black"; 
      document.getElementById("button2").style.backgroundColor ="gray"; 
     }; 

document.getElementById("button2").onclick = function(){ 
      this.style.backgroundColor ="black"; 
      document.getElementById("button1").style.backgroundColor ="gray"; 
     }; 
+0

実際に働くだけで答えます、ありがとうございます。私が今持っている1つの問題は、私がそれを解決するのを助けてくれてありがたく思っています。どちらかのボタンをクリックした後にボタンの上にマウスを置くと、上に置かれても色の変化はなくなります。また、私のスクリプトでは、.button:hover {background-color:black;}を持っていました。これは、ボタンがクリックされる前に機能しますが、いずれかのボタンがクリックされるとすぐに、どちらのボタンにもホバー効果はありません。 – H3ll0

0

背景を設定したクラスを追加するには、jquery addClass関数を使用します。 クラスは、ページが読み込まれるまで残ります。

CSSアクティブなプロパティはアクティブですが、ボタンでは機能しません。なぜなら、アクティブなプロパティはクリックボタンを放すと消えるからです。たぶんボタンとして装飾されたタグを使用すると動作するかもしれませんが、アクティブ状態を失ったら消えてしまいます。

0

jQueryを使用すると、簡単に実行できます。

$('#buttons .item').on("click",function(){ 
 
    $("#buttons .item.active").removeClass('active'); 
 
    $(this).addClass("active"); 
 
});
button{ 
 
    color: white; 
 
} 
 
.item{ 
 
    background-color: gray; 
 
} 
 
.active{ 
 
    background-color: black; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="buttons"> 
 
    <button class="item">Button 1</button> 
 
    <button class="item">Button 2</button> 
 
    <button class="item">Button 3</button> 
 
</div>

1

ここで私はノーJavaScriptで手早く例だ - 代わりに私が「確認」されている1に応じてスタイルされている2つのラジオボタンを使用していますが。

http://codepen.io/capitalq/pen/gLWLMK

.button { 
 
    -webkit-appearance: none; 
 
    appearance: none; 
 
    border: 0; 
 
    background: gray; 
 
    height: 50px; 
 
    width: 50px; 
 
    margin: 10px; 
 
    outline: none; 
 
    display: inline-block; 
 
    border-radius: 50%; 
 
} 
 

 
.button:checked { 
 
    background: black; 
 
}
<input type="radio" class="button" name="buttons" id="button1" /> 
 
<input type="radio" class="button" name="buttons" id="button2" />

+1

ボタンが実際に動作するまで、いいアイデアです。この時点で、ボタンのアクションまたはアクティブな状態を格納するために 'js'が必要になります。私はあなたの挑戦が好きなので、 'js 'なしで両方を行う方法を考えます。しかし、思考のための+1は、ボックスをoutsude。 –

+0

それをチェックしてください。箱から考えてください。 Chapeau。 –

0
<style media="screen"> 
    #buttonA, #buttonB { 
     display: inline-block; 
     height: 10px; 
     width: 10px; 
     border-radius: 10px; 
     background-color: gray; 
    } 
</style> 

<a id="buttonA"></a> 
<a id="buttonB"></a> 

<script type="text/javascript"> 

    var buttonA = document.querySelector('#buttonA'); 
    var buttonB = document.querySelector('#buttonB'); 

    var changeColour = function (e) { 

     if (e.target === buttonA) { 
      buttonA.style.backgroundColor = 'black'; 
      buttonB.style.backgroundColor = 'gray'; 
     } 

     if (e.target === buttonB) { 
      buttonB.style.backgroundColor = 'black'; 
      buttonA.style.backgroundColor = 'gray'; 
     } 

    }; 

    buttonA.addEventListener('click', changeColour, false); 
    buttonB.addEventListener('click', changeColour, false); 

</script> 
0

HTML button要素があります。だから、あなたはあなたのページ上のボタンをマークアップしたい場合は、あなたが本当に使用するべきである:ここで

<button type="button"></button> 

はJavaScriptで<button>classListを使用してのアプローチです:

var buttons = document.getElementsByTagName('button'); 
 

 
function paintItBlack() { 
 

 
    for (var i = 0; i < buttons.length; i++) { 
 
    buttons[i].classList.remove('clicked'); 
 
    } 
 

 
    this.classList.add('clicked'); 
 
} 
 

 
for (var i = 0; i < buttons.length; i++) { 
 
    buttons[i].addEventListener('click',paintItBlack,false); 
 
}
.clicked { 
 
color: rgb(255,255,255); 
 
background-color: rgb(0,0,0); 
 
}
<button type="button">Button A</button> 
 
<button type="button">Button B</button>

0

JavaScriptは現時点では不要です。このコードは、.cssファイルまたはhtmlファイルの "style"タグで使用します。マウスが上にあるときとクリックしたときに黒くなります。

a:hover 
    { 
     background-color:black; 
    } 
    a:target 
    { 
     background-color:black; 
    } 
+0

これはすべてのリンクではなく、 'ボタン'にのみ適用します。私はそれを試して、それは動作しません。効果はまったくありません。 – H3ll0

+0

を#ボタンに変更します。 –

関連する問題