I need to have a function run once when the page is loaded, and then again whenever a button is pressed. If I take out the code to run it on page load, it works whenever the button is pressed, otherwise it only runs once on page load and never when the button is pressed.
$(function()
{
// code to be run
});
$(document).ready(function()
{
$("#b开发者_C百科utton").click(function()
{
// code to be run
});
});
function run(){
//code to run
}
$(document).ready(function()
{
$("#button").click(function()
{
run()
});
run()
});
Define a function:
$(function() {
var your_code = function() {
// code to be run
};
$("#button").click(your_code);
your_code();
});
Working Example.
精彩评论