Phenomenon: cannot invoke functions defined in ready in jquery
Source: <script type= "Text/javascript" >
$ (document). Ready (function (e) {
function Test () {
Alert (' test! ');
}
});
Test (); Error, test () not defined
</script>
Parse: Ready is also equivalent to a function, that is, a new local function scope, outside of course not available. The same as the OnLoad function of JS, such as onload= "Ready ()"
function Ready () {
function Test () {
Alert ("test!")
}
}
You won't be able to call the test () function outside.
Workaround:
1, the function to be called outside the test () from $ (document). Ready to be extracted separately for the call!
2. The scope of the function that will be called externally is promoted to the global scope (variable declaration), as follows:
<script type= "Text/javascript" >
var test;//defines a global variable that can be omitted
$ (document). Ready (function (e) {
Test=function () {
Alert (' test! ');
}
});
Test (); It is now possible to call outside the
</script>
<a href= ' javascript:; ' onclick= ' Test (); ' > Click Testing, Test (); Call for tag events </a>