简体   繁体   English

如何使用Jquery触发onload事件?

[英]how to fire an onload event with Jquery?

I am having a div and I want to style that div on page load with Jquery. 我有一个div,我想用Jquery在页面加载上设置div。 The popup works fine But how can I fire it when the page loads. 弹出窗口工作正常但是如何在页面加载时触发它。

<div id="popup">
   //Some content 
</div>

Below is the Jquery Code 下面是Jquery代码

$(document).ready(function() {
        $('div#popup').load(function() {
                           // Popup Content
}
}

I even tried a simple alert message but doesnt work. 我甚至尝试了一个简单的警报消息,但不起作用。

Thanks 谢谢

If you just want to style it do this: 如果你只想要它的风格,它会这样做:

$(document).ready(function() {
     $('div#popup').css({
           width: '100px',
           height: '100px' //etcetera
     });
});

All you need is the outer wrapper: 你需要的只是外包装:

$(document).ready(function() {
    //do something here
}

The $(document).ready closure doesn't execute until the entire DOM has been loaded. 在加载整个DOM之前, $(document).ready闭包不会执行。

If you're using the jQuery UI Modal dialog then just make sure autoOpen isn't set to false in your dialog constructor. 如果您正在使用jQuery UI模式对话框,那么只需确保对话框构造函数中的autoOpen未设置为false You'll notice that by default the autoOpen attribute is set to true so the standard behavior is to pop up the dialog automatically on page load. 您会注意到默认情况下autoOpen属性设置为true因此标准行为是在页面加载时自动弹出对话框。 If this isn't happening, debug your page and be certain that your dialog constructor is being properly initialized on document ready. 如果没有发生这种情况,请调试页面并确保在文档就绪时正确初始化了对话框构造函数。

Check out the "Options" tab on the jQuery UI demo page . 查看jQuery UI演示页面上的“选项”选项卡。 The second option autoOpen contains documentation on the default behavior of the jQUI dialog's popup behavior. 第二个选项autoOpen包含有关jQUI对话框弹出行为的默认行为的文档。

The following code would be all it would take to do what you want to pop up a modal window. 以下代码将是您想要弹出模态窗口所需的全部内容。 Remove the options parameter altogether if you need to to remove the modal behavior: 如果需要删除模态行为,请完全删除options参数:

$(function() {
    $("div#popup").dialog({
         modal: true
    });
});

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

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