You've got it nearly correct, but you haven't accounted for the this
value supplied to the inline code.(你已经几乎正确了,但你没有考虑提供给内联代码的this
值。)
<a href="#" onclick="alert(this)">Click Me</a>
is actually closer to:(实际上更接近:)
<a href="#" id="click_me">Click Me</a>
<script type="text/javascript">
document.getElementById('click_me').addEventListener("click", function(event) {
(function(event) {
alert(this);
}).call(document.getElementById('click_me'), event);
});
</script>
Inline event handlers set this
equal to the target of the event.(内联事件处理程序this
设置为等于事件的目标。) You can also use anonymous function in inline script(您还可以在内联脚本中使用匿名函数)
<a href="#" onclick="(function(){alert(this);})()">Click Me</a>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…