the event onclick
is bound in the way of native JavaScript object binding, but the actual effect is similar to that of onload
.
wants a mouse click button pop-up prompt, but it actually pops up as soon as is loaded on the page. click has no effect .
to do this, I tried three ways:
-
The
- real name function is called in the button property ( property binding ). OK!
- Anonymous function, property binding and object binding are OK! The error mentioned above occurs when
- adapts the function and assigns a value to the button object"s property
onclick
( object binding ). Error
Please see the code film:
1. Object binding event ( failed )
////
<button id="b" ></button>
<script type="text/javascript">
//fun(), //
function fun() {
alert("xxxx");
}
document.getElementById("b").onclick = fun(); //?: onclick onload?
2. Anonymous function binding ( successful )
////
<button id="b" ></button>
<script type="text/javascript">
//fun(), //
document.getElementById("b").onclick = function(){
alert("xxxx");
}
3. Real name function, attribute binding ( successful )
//buttononclickfun()//
<button id="b" onclick="fun()"></button>
<script type="text/javascript">
////
function fun() {
alert("xxxx");
}
Why does the first approach fail? What is the mechanism that causes this?