我正在写一个jQuery插件,但我有一个问题:Click事件不会在jQuery插件正常工作
我的HTML元素
<a id="trac"></a>
我的JS调用插件
$('#trac').myplugin();
我的插件
$.fn.myplugin = function(){
var $root;
return this.each(function(){
$root = $(this);
$root.live('click',function(){
console.log('here');
});
});
}
发生“从这里”永远不会显示。 但如果我使用...
$('#trac').live('click',function(){
console.log('here');
});
... “这里” 显示。 我不明白为什么会这样,因为$ root和$('#trac')是完全相同的jQuery对象。
我该如何解决?
谢谢!
之前你的插件加载它被调用,你使用'document.ready()'arround'$('#trac')。myplugin();'? – powtac
你想要插件做什么?你为什么使用“.live()” ? – Pointy
是的,它被加载到一个document.ready()中 –