jQuery jquery初始化函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2013810/
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
jquery init function
提问by Val
I got a simple UI widget and i'd like it to alert() the "hello world!!!" when initialised.
我有一个简单的 UI 小部件,我希望它能够提醒()“你好世界!!!” 初始化时。
$.widget("ui.myWidget", {
_init: function() {
//start
alert('Hello World!!!');
}
}
could some one explain how this init or _init function works.?
if i was to call the ui just like the dialog ui would $('#selector').dialog()
in other words if a click <div onclick="$('#id').myWidget()">Click Me</div>
then i should get the alert to pop up.
有人可以解释这个 init 或 _init 函数是如何工作的吗?如果我要像对话框 ui 一样调用 ui $('#selector').dialog()
,换句话说,如果单击,<div onclick="$('#id').myWidget()">Click Me</div>
那么我应该弹出警报。
this code is short because i didnt want to put a lot of coding into it so its easy to read. thanks
这段代码很短,因为我不想在里面写很多代码,所以很容易阅读。谢谢
采纳答案by PetersenDidIt
Yes when you call $('#id').myWidget(); it will trigger your _init function to run and fire the alert.
是的,当你调用 $('#id').myWidget(); 它会触发您的 _init 函数运行并触发警报。
Here is a short postabout using the jQuery UI widget factory
这是一篇关于使用jQuery UI 小部件工厂的简短帖子