我正在编写一个jQuery插件,但是遇到了一个问题:
我的HTML元素
<a id="trac"></a>
我的JavaScript调用插件的代码
$('#trac').myplugin();
我的插件
$.fn.myplugin = function(){
var $root;
return this.each(function(){
$root = $(this);
$root.live('click',function(){
console.log('here');
});
});
}
有时候,“here”这个词从未被显示出来。 但是如果我使用...
$('#trac').live('click',function(){
console.log('here');
});
“这里”被显示出来了。 我不明白为什么会发生这种情况,因为$root和$('#trac')是完全相同的jQuery对象。
我该如何解决呢?
谢谢!
$('#trac').myplugin();
周围使用了document.ready()
? - powtac