简体   繁体   English

jQuery函数在页面上调用onload和onclick吗?

[英]jquery function call on page both onload and onclick?

i am working on jquery calender but i need to call a function both on page load and then on click. 我正在使用jquery日历,但是我需要在页面加载和单击时都调用一个函数。 when the page load for the first time it should automatically call and execute the function and for 2nd time and onward it should be call through click. 首次加载页面时,应自动调用并执行该函数;第二次及以后,则应通过点击进行调用。 is it possible?? 可能吗?? if yes please help me. 如果是,请帮助我。

   <script type="text/javascript">

   $(document).ready(function(){
      $("#fullDate").datepicker({

        showOn: "button",
        buttonImage: "images/calender.jpg",
        buttonImageOnly: true,

        onClose: function(dateText, inst) {
        $('#year').val(dateText.split('/')[2]);
        $('#month').val(dateText.split('/')[0]);
        $('#day').val(dateText.split('/')[1]);
    }


    });


});
</script>

Try writing a named function and call it on both events: 尝试编写一个命名函数,并在两个事件上调用它:

function doSomething() {
    // ...
}
$(document).ready(function() { doSomething() });
$(yourElem).on("click", function() { doSomething() });

Alternatively to writing an anonymous function as a callback, just to call doSomething , you could also pass the function name as an argument, gaining a little performance by not cluttering memory with unnecessary anonymous functions: 除了将匿名函数编写为回调之外,仅调用doSomething ,您还可以将函数名称作为参数传递,通过不因不必要的匿名函数而使内存混乱,从而获得一些性能:

$(document).ready(doSomething);
$(yourElem).on("click", doSomething);
$(document).ready(function(){
  $("#fullDate").datepicker({
    showOn: "button",
    buttonImage: "images/calender.jpg",
    buttonImageOnly: true,

    onClose: function(dateText, inst) {
        $('#year').val(dateText.split('/')[2]);
        $('#month').val(dateText.split('/')[0]);
        $('#day').val(dateText.split('/')[1]);
    }
  });
  $("#fullDate").datepicker("show"); // this will run once.
});

During load, you can use 在加载期间,您可以使用

//when window loads
$(window).load(function(){
    functionCallHere();
});

//or when DOM is ready
$(document).ready(function(){
    functionCallHere();
});

Then for the click 然后点击

$(element).click(function(){
    functionCallHere();  //call the function again
});

Where functionCallHere is the name of the common function called on those events. 其中functionCallHere是在这些事件上调用的通用函数的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM