调用jquery函数onload

时间:2015-01-03 16:29:39

标签: jquery

我有以下jquery函数,当我点击一个按钮时显示一个信息框,这很好但我希望能够在页面加载时显示相同的信息框(无需按下按钮) 。但它不起作用,我做错了什么?

    // notification
if(jQuery('#growl2').length > 0) {
    jQuery('#growl2').click(function(){
        var msg = "Tip: Copy partnumber by searching for an exsisting and select it from grid.";
        jQuery.jGrowl(msg, { life: 5000});
    });
}

这是在身体结束前放置的......

 <script>
 $(document).ready(function () {
     var msg = "Tip: Copy partnumber by searching for an exsisting and select it from grid.";
     jQuery.jGrowl(msg, { life: 5000 });
 });

仅当我点击此按钮时才有效..

<a id="growl2" class="btn btn-default"><small>Show message</small></a>

3 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

function loadjGrowl() {
   var msg = "Tip: Copy partnumber by searching for an exsisting and select it from grid.";
   jQuery.jGrowl(msg, { life: 5000 });
}

window.onload = loadjGrowl;

注意:当页面完全加载时会触发此操作。

答案 1 :(得分:0)

如果你的按钮点击处理程序工作正常,你只需在没有函数参数的情况下调用.click()来附加它后立即激活它:

if(jQuery('#growl2').length > 0) {
    jQuery('#growl2').click(function(){
        var msg = "Tip: Copy partnumber by searching for an exsisting and select it from grid.";
        jQuery.jGrowl(msg, { life: 5000});
    }).click();   //  <---- this
}

答案 2 :(得分:0)

当您将代码从body之前移动到head之前。

如果通过在body标记之前放置jQuery.js来推迟加载jQuery,也可能会出现此问题。

因此,要么在head标记之前移动代码,要么在head标记

之前移动<script src="jQuery.js" ></script>

或者你改用

<script>
window.onload = function(){
    var msg = "Tip: Copy partnumber by searching for an exsisting and select it from grid.";
     jQuery.jGrowl(msg, { life: 5000 });
}
</script>