letting jquery reload itself
How can you let jquery reload itself? when i puch a button i would like to tell jquery that it 开发者_Go百科should run the .ready function again.Whats the best way to do this.
You can't do this, when jQuery loads and runs through all ready
handlers, it also clears them off the list, so they're not available to run again.
Instead, put your content in another function you can call, for example:
function startUp() {
//do stuff
}
$(startUp); //run on ready
Then whenever you need, call startUp()
to execute it again.
Well, you could run the whole code again. You probably don't want to, however. For instance, if you ran the whole document.onready
code again, you would rebind the click handler to the button you mentioned in your question. This would mean an ever-increasing number of handlers bound to that element -- when you click it, it would run the handler once the first time, then twice the second time, three times the third time, etc.
You need to separate out the code that you need to be done once and the code that you need to run multiple times. You could then do something like this:
function init() {
// do all the stuff you want done multiple times
}
$(document).ready(function(){
init(); // do the repeatable stuff
$('#myButton').click(init); // run init() when you click on the button
// any other once-only code
});
精彩评论