【问题标题】:adding ui with jquery plugin not working使用 jquery 插件添加 ui 不起作用
【发布时间】:2012-08-10 13:06:27
【问题描述】:
我的插件
$.fn.myplugin=function(){
var element=$('<div/>').addClass('select').appendTo(this);
return this;
}
和脚本
$('<div/>').myplugin().appendTo('body');
问题是元素没有附加。
【问题讨论】:
标签:
jquery
jquery-plugins
jquery-selectors
【解决方案1】:
它按原样工作:http://jsfiddle.net/7n2Bd/
但是如果你传递一个元素的集合,你就会遇到问题。试试这个:
$.fn.myplugin=function() {
return this.each(function() {
$('<div>').addClass('select').appendTo(this);
});
};
【解决方案2】:
添加准备好的文档也可以在这里提供帮助
$(document).ready(function() {
$('<div/>').myplugin().appendTo('body');
});
【解决方案3】:
看来你的代码没问题。
$.fn.myplugin = function() {
// hello is for just view purpose
$('<div>hello</div>').addClass('select').appendTo(this);
return this;
}
$('<div/>').myplugin().appendTo('#target'); // here instead of '#target' use 'body'
DEMO
供收藏
$.fn.myplugin = function() {
return $.each(this, function() {
$('<div>hello</div>').addClass('select').appendTo(this);
});
}
注意
将所有代码放入$(document).ready({ .. })。