在这里,我们有一个onclick处理一个div:HTML中支持内嵌JavaScript事件子标记吗?
<div id="BorderContainer1282" onclick="alert('hello')">
<input id="Button925" type="button" value="Button">
</div >
当点击它显示一个警告对话框。
不支持HTML内嵌JavaScript同名的子标签,就像这样:
<div id="BorderContainer1282">
<onclick>alert('hello');</onclick>
<input id="Button925" type="button" value="Button">
</div >
这个问题是不是最好的做法。具体来说,HTML支持在HTML元素的子标记中设置事件,如示例中所示。
注意:我在示例2中测试了上面的代码,它在我的测试中不起作用。
如果它不能正常工作,会使用以下模板是一个可以接受的选择:
<div id="BorderContainer1282">
<script type="text/javascript">
document.getElementById('BorderContainer1282').addEventListener("click", function(event) {
(function(event) {
alert('hello');
}).call(document.getElementById("BorderContainer1282"), event);
});
</script>
<input id="Button925" type="button" value="Button">
</div >
同样,我现在不担心最佳实践,只有在支持此功能。
我发现了一个类似的问题here这不是重复的。
''标签?作为一个属性,它已经很丑陋,但是...... Ps:从来没有在HTML中看到过这个,但也许很久很久以前? PPs:你最近的例子中'call()'的用途是什么? –
Kaiido
我认为这个调用导致'this'引用引用HTML元素而不是另一个对象。 –
错误的猜测,'this'指的是事件已附加到的对象。在这里你调用'undefined'(addEventListener()')的结果) – Kaiido