簡體   English   中英

javascript鼠標事件

[英]javascript mouse events

javascript中是否有一種方法可以使“onclick”適用於特定類的所有成員? 因此,如果我有對象 A 和 B 都是 X 類型,單擊它們中的任何一個都會調用相同的函數? 但是該函數應該只對調用 A 或 B 中的任何一個起作用,而不是同時起作用。

因為基本上我想要做的是當我點擊 A 或 B 時,它們會移動到不同的位置,但我希望這能夠適用於同一類中任意數量的 n 個元素

演示: http : //jsfiddle.net/2BRS9/

為所有這些 html 元素添加相同的類。 您可以獲取事件所在的元素

如果類名是“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);

   }

});

   <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");
 });

試試這個:

$('.classname_of_member').bind('click',function(){
//your code here
});

為此,您可以使用“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.
});

我編寫了一些簡單的代碼來處理鼠標操作,包括單擊、雙擊、右鍵單擊和拖放。 [www.derbyshiresoftware.co.uk/how-to-handle-mouse-operations/][1]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM