我为jQuery编写了一个插件,但现在我的用户要求能够在同一页面上的各种元素上运行我的插件,所以我试图重写一下.
我的活动有问题.
这是什么工作.
$(".jTagLabels label").live('mouseenter',function(){
$("#"+$(this).attr('rel')).css('opacity',1).find("span").show();
$(".jTagDeleteTag").show();
});
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试在特定的上下文中注册此事件,但它不起作用.
$(".jTagLabels label",container).live('mouseenter',function(){
$("#"+$(this).attr('rel')).css('opacity',1).find("span").show();
$(".jTagDeleteTag",container).show();
});
Run Code Online (Sandbox Code Playgroud)
console.log(container)返回正确的dom元素......
可能很重要的是,当我设置事件时,没有创建jTagLabel,这就是我使用live()函数的原因......
使用delegate(),它更清洁,这就是它的用途,即使生成的代码可能是相同的.
此外,根据您的代码上下文,您的container变量可能会在绑定事件和触发事件之间失去其值.
$(container).delegate(".jTagLabels label",'mouseenter',function(){
var $this = $(this);
$("#"+$this.attr('rel')).css('opacity',1).find("span").show();
// There should be an easier way to find your container,
// like .closest('.someclass')
$this.parent().parent().find('.jTagDeleteTag').show();
});
Run Code Online (Sandbox Code Playgroud)