我正在尝试获取一个元素的 ID,就像一个基本过程一样,但它没有找到任何东西(未定义)。编码:
function reserve() {
alert(this.id);
};
<a id="qweqwe" onclick="reserve();" href="#openModal2">тратата</a>
所以同样不起作用:
alert($(this).attr('id'));
包含 jQuery
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
在你的例子中:
this
将是一个对象window
。要获取您单击的当前元素,您需要onclick="reserve(**this**);"
传递this
.接着:
会给你一个id。
完整示例代码: