我在理解onclick的功能时遇到了问题。例如:-
HTML
<input type="button" onclick="yetAnotherAlert()" />
JS
<script type="text/javascript">
function yetAnotherAlert(textToAlert) {
alert(textToAlert);
}
yetAnotherAlert("This is Chapter 2");
</script>
在这里,我期望一旦单击按钮,就会调用函数yetAnotherAlert()
。但当我在Chrome浏览器中打开页面时,它甚至不用点击按钮就触发了[点击功能在这里确实有效]。我的问题是:-为什么在加载页面之前会触发该功能
因为您在页面加载期间调用函数
yetAnotherAlert("This is Chapter 2");
删除它。你必须在点击事件中传递字符串,比如这个
<input type="button" onclick="yetAnotherAlert('This is Chapter 2')" />