<p id="specialp">some content</p>
<script>
document.getElementById('specialp').onclick=alert('clicked');
</script>
I'm just starting out with Javascript, and I don't understand why the alert is executed when page loads, but not when I click that paragraph.
The handler works as I expect when I put it inline, like this:
<p id="specialp" onclick="alert('clicked')" >some content</p>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…