单击四个按钮的单个事件处理程序 [英] Single event handler for four button clicks
本文介绍了单击四个按钮的单个事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是javascript新手.如何为四个按钮使用单个事件处理程序. html和js都应该在同一页面中.预先感谢.
I am new in javascript. How to use single event handler for four buttons. Both html and js should be in single page. Thanks in advance.
<html>
<head>
<script>
myFunc() {
var fired_button = $(this).val(); alert(fired_button);
}
</script>
</head>
<body>
<input type="button" value="one" onClick="myFunc()"><br>
<input type="button" value="two" onClick="myFunc()"><br>
<input type="button" value="three" onClick="myFunc()"><br>
<input type="button" value="four" onClick="myFunc()"><br>
</body>
</html>
推荐答案
由于使用的是jquery
,因此不需要onclick
属性,只需添加一个与所有按钮相似的类即可.并且请勿忘记在所有js
文件上方添加jquery
的js
文件
Since you are using jquery
, you don't need the onclick
attribute, just add a class that is similar for all buttons. And DO NOT forget to add the js
file of jquery
above all js
files
$('.someClass').click(function() {
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="someClass" type="button" value="one"><br>
<input class="someClass" type="button" value="two"><br>
<input class="someClass" type="button" value="three"><br>
<input class="someClass" type="button" value="four"><br>
这篇关于单击四个按钮的单个事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文