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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5027169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:19:05  来源:igfitidea点击:

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

javascriptjquery

提问by Abhi

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 i m using jBar plugin to show stackoverflow type notifications at top of the page.

实际上我使用 jBar 插件在页面顶部显示 stackoverflow 类型的通知。

回答by Reigel

$(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...
 });

回答by Curt

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

回答by Sarfraz

I thinkyou 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 更新之后也是如此。

回答by Abhi

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>";

回答by Guffa

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.

该插件仅用于通过单击元素来使用,不支持在没有单击事件的情况下显示栏。

Reigel's suggestion to call clickimmediately 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立即调用应该可行,但是您必须在页面中包含该元素,以便插件可以将事件连接到某些内容。

回答by Prakash

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. Create element having id msgup and modify code,

Abhishek,您的代码似乎使用了一些 jquery 插件,该插件将对具有 id msgup 的元素执行某些操作,您应该具有具有 id msgup 的元素。创建具有 id msgup 的元素并修改代码,

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