[英]javascript mouse events
Is there a way in javascript to make it so that an "onclick" works for all members of a particular class? javascript中是否有一种方法可以使“onclick”适用于特定类的所有成员? So if I have objects A and B that are both of type X, clicking on either of them will call the same function?
因此,如果我有对象 A 和 B 都是 X 类型,单击它们中的任何一个都会调用相同的函数? But that function should only work on whichever of A or B was called, not both at the same time.
但是该函数应该只对调用 A 或 B 中的任何一个起作用,而不是同时起作用。
Because basically what I'm trying to do is when I click on either A or B they move to a different location, but I want this to be able to work for any arbitrary number of n elements in the same class因为基本上我想要做的是当我点击 A 或 B 时,它们会移动到不同的位置,但我希望这能够适用于同一类中任意数量的 n 个元素
DEMO : http://jsfiddle.net/2BRS9/演示: http : //jsfiddle.net/2BRS9/
Add the same class for all those html elements .为所有这些 html 元素添加相同的类。 You can get the element from which the event was fi
您可以获取事件所在的元素
If the classname is "myclass"如果类名是“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");
});
Try this:试试这个:
$('.classname_of_member').bind('click',function(){
//your code here
});
For this you can make use of "this" keyword.为此,您可以使用“this”关键字。
for eg例如
<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.
});
I have written some simple code to handle mouse operations including click, double click, right click and drag and drop.我编写了一些简单的代码来处理鼠标操作,包括单击、双击、右键单击和拖放。 [www.derbyshiresoftware.co.uk/how-to-handle-mouse-operations/][1]
[www.derbyshiresoftware.co.uk/how-to-handle-mouse-operations/][1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.