我试图从使用单击函数的按钮获取 id,而无需将对象传递给该函数。我一直得到未定义而不是 id,我不知道为什么,我读过的所有内容都说这应该有效。
<input type="button" value="1" id="one" onclick="return A();"/>
<input type="button" value="2" id="two" onclick="return A();"/>
<input type="button" value="3" id="three" onclick="return A();"/>
<script type='text/javascript'>
function A(){
var but = $(this).attr("id");
alert(but);
}
</script>
最佳答案
<input type="button" value="3" id="three" onclick="return A(this);"/>
function A(ele){
var but = $(ele).attr("id");
alert(but);
}
但是,由于您已经在使用 jQuery...您也可以这样做:
<input type="button" value="3" id="three" class="buttonClick"/>
$('.buttonClick').on("click", function() {
var but = $(this).attr("id");
alert(but);
});
关于javascript - 获取使用该功能的按钮的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21631334/