[英]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.