jquery widget help
given: jquery 1.3.2 jquery ui 1.7.2
I found the following jquery snippet but am ha开发者_开发知识库ving problems with it.
$(document).ready(function() {
$.widget("ui.hello", {
options: {
foo: "bar",
baz: "quux"
},
_init: function() {
var text = this.options.text;
this.element.innerHTML("hello " + this.options.foo + " and " + this.options.baz);
}
});
$("#thing").hello({ foo: "WORLD!" });
});
Here's the problem:
this.options.baz); is undefined shouldn't it be using the default?
source: http://blog.citrusbyte.com/2010/09/16/jquery-widgets-bringing-sanity/
this
probably does not refer to the widget due to call context when the _init function is called. Try removing the this.
and just refer to options like so:
this.element.innerHTML("hello " + options.foo + " and " + options.baz);
Here's what i ended up doing:
<script type="text/javascript">
$(document).ready(function() {
$.widget("ui.hello", {
_init: function() {
var text = this.options.text;
alert(this.options.baz);
}
});
$.extend($.ui.hello, {
defaults: {
baz: "whatever"
}
});
$("#thing").hello({ foo: "WORLD!" });
});
</script>
精彩评论