0

特定のクラスのすべてのメンバーに対して「onclick」が機能するようにする方法はjavascriptにありますか? では、両方ともタイプ X のオブジェクト A と B がある場合、どちらかをクリックすると同じ関数が呼び出されるのでしょうか? ただし、その関数は、A または B のどちらかが呼び出されたときにのみ機能し、同時に両方では機能しません。

基本的に私がやろうとしているのは、AまたはBのいずれかをクリックすると別の場所に移動することですが、同じクラス内の任意の数のn要素に対してこれを機能させたいからです

4

5 に答える 5

0

これには、「this」キーワードを使用できます。

例えば

<input type="button" class="abc" value="A" />
<input type="button" class="abc" value="B" />
<input type="button" class="abc" value="C" />
<input type="button" class="abc" value="D" />


$(".abc").click(function() {
 $(this).hide(); // for hiding the only the clicked element.
});
于 2012-11-01T10:12:06.230 に答える
0

デモ: http://jsfiddle.net/2BRS9/

これらすべての html 要素に同じクラスを追加します。イベントがfiだった要素を取得できます

クラス名が「myclass」の場合

  $(".myclass").live({
      click: function(e){
      // You can get id by many ways, some shown here :
      id= this.id;
      console.log(id);
      id = $(this).attr("id");
      console.log(id);
      id = e.target.id;  
      console.log(id);

   }

}); </p>

于 2012-11-01T10:06:08.710 に答える
0
   <input type="button" class="abc" value="A" />
   <input type="button" class="abc" value="B" />
   <input type="button" class="abc" value="C" />
   <input type="button" class="abc" value="D" />


 $(".abc").click(function() {
  alert("any of the above is clicked");
 });
于 2012-11-01T10:06:17.930 に答える
0

これを試して:

$('.classname_of_member').bind('click',function(){
//your code here
});
于 2012-11-01T10:06:30.407 に答える