简体   繁体   English

如何在jQuery中的文档就绪事件上自动调用函数

[英]How to call a function automatically on document ready event in jQuery

I m having following Code in which this code executes when i click a anchor tag named msgup 我有以下代码,当我单击名为msgup的锚标记时,该代码将在其中执行

    $("#msgup").bar({
        color : '#1E90FF',
        background_color : '#FFFFFF',
        removebutton     : false,
        message : 'Your profile customization has been saved!',
        time: 4000

});

But I want to do this thing automatically when page loads , so what is required to achieve such workflow? 但是我想在页面加载时自动执行此操作,那么要实现这种工作流程需要什么?

Actually im using jBar plugin to show stackoverflow type notifications at top of the page. 实际上,我使用jBar插件在页面顶部显示stackoverflow类型通知。

$(document).ready(function() {

    $("#msgup").bar({
        color : '#1E90FF',
        background_color : '#FFFFFF',
        removebutton     : false,
        message : 'Your profile customization has been saved!',
        time: 4000

    }).click(); // call click right away...
 });

Put the code into a function, and then run that function in your jquery document ready: 将代码放入函数中,然后在您的jquery文档中运行该函数:

function msgup_bar(){
$("#msgup").bar({
        color : '#1E90FF',
        background_color : '#FFFFFF',
        removebutton     : false,
        message : 'Your profile customization has been saved!',
        time: 4000

});
}

$(document).ready(function() {
   msqup_bar();
 });

http://www.learningjquery.com/2006/09/introducing-document-ready http://www.learningjquery.com/2006/09/introducing-document-ready

I think you are looking for LiveQuery jquery plugin . 认为您正在寻找LiveQuery jquery插件

Live Query utilizes the power of jQuery selectors by binding events or firing callbacks for matched elements auto-magically, even after the page has been loaded and the DOM updated. Live Query通过自动绑定事件或触发匹配元素的回调来利用jQuery选择器的功能,即使在页面加载和DOM更新之后也是如此。

I have done this using following code 我使用以下代码完成了此操作

$(document).ready(function()
{

     $("<div id='notify'></div>").prependTo('body').hide();
}

Style defined in header: 标头中定义的样式:

<style>
#notify{

       position:fixed;
           top: 21%; 
        left: 40%;
       width: 20%;
        height:4%;
        text-align:center;
            text-weight:bold;
           font-size:20px;
         background-color:YELLOW;
          color:BLUE;
            padding:3px;
           z-index:9999;
}

</style>

and dynamically call this function using following echo statement in PHP 并在PHP中使用以下echo语句动态调用此函数

echo "<script> setTimeout(function(){
            $('#notify').html('Updated Successfully !').slideDown(2000);
 },0);
     </script>";

The plugin is only built to be used by clicking on an element, there is no support for showing a bar without a click event. 该插件只能通过单击某个元素来使用,不支持显示没有click事件的栏。

Reigel's suggestion to call click immediately should work, but then you have to include that element in the page so that the plugin has something to hook up the events to. Reigel建议立即调用click应该可以起作用,但是随后您必须在页面中包含该元素,以便插件可以将事件关联到其中。

Abhishek, your code seems it uses some jquery plugin, which will do something with the element having id msgup, you should have element having id msgup. Abhishek,您的代码似乎使用了一些jquery插件,该插件将对ID为msgup的元素进行处理,而您应该对ID为msgup的元素进行处理。 Create element having id msgup and modify code, 创建具有ID msgup的元素并修改代码,

$(document).ready(function(){
//you code here
})

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

相关问题 如何从 document.ready 调用 jQuery 函数 - How to call jQuery function from document.ready 如何使用setTimeout()调用jQuery(document).ready之外的函数? - How to call function outside of jQuery(document).ready with setTimeout()? 如何从jquery中准备好的文档中调用ajaxComplete内部的函数? - How to call function inside ajaxComplete from document ready in jquery? 如何在 document.ready 上调用多个 js 函数而不放置它们 jQuery(document).ready(function(){}); - How to call multiple js functions on document.ready without putting them jQuery(document).ready(function(){}); jQuery文档已准备就绪,并按键可以调用一个函数 - jQuery document ready and keypress to call one function 如何在$(document).ready之后调用带有参数的函数 - How to call a function with parameters after $(document).ready 如何调用$(document).ready()中声明的函数? - How to call function declared in $(document).ready()? 如何使用 $(document).ready(function() 和另一个事件 Function 运行 Javascript/Jquery Function? - How to run a Javascript/Jquery Function using both $(document).ready(function() and Another Event Function? 从jQuery中的document.ready调用onload事件 - Call onload event from document.ready in jQuery 如何在jQuery中使用文档准备功能重定向 - How to redirect with document ready function in jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM