The jquery binding Click event was discovered today when the front page was developed. The code is as follows:
1.html:<input id= "ceshisub" type= "button" value= "click event" >
The method function in the test.js referenced by 2.html:
Copy Code code as follows:
$ ("#ceshisub"). Bind ("click", Function () {
var a=1;
a +=1;
Alert ("Ceshisub");
});
The problem is to solve the "click event" button in the page without any response, open the JS Debugging window in Var a=1; The breakpoint in this line is not coming in.
The solution is:
First, add the loading event on the JS function above:
The following code is added:
Copy Code code as follows:
$ (function () {
$ ("#ceshisub"). Bind ("click", Function () {
var a=1;
a +=1;
Alert ("Ceshisub");
});
});
In this case, the binding event takes effect.
JS has a total of three load functions, in addition to the above
Copy Code code as follows:
$ (function () {
Alert ("1th method. ");
});
There are two other ways to do this:
Copy Code code as follows:
Window.onload=function () {
Alert ("2nd method. ");
}
$ (document). Ready (function () {
Alert ("3rd method. ");
});
Second, if you do not use the JS load function to initialize the binding event, there is another way:
Statement that will refer to JS
<script language= "javascript" src= "/js/test.js" ></script>
Put it on the last side of the page to load.
Summarize:
jquery When you bind an event on an element, it first looks up the element A in docment, and the binding fails if it is not found.
The first solution above is to bind when initializing JS when the page is initialized
The second way is to ensure that the page elements are initialized after the completion of the binding, when all the elements have been initialized, must be bound.