【发布时间】:2011-08-13 23:32:24
【问题描述】:
目前我有以下使用颜色框插件http://colorpowered.com/colorbox/的jQuery
$(document).ready(function(){
$(".altbgcolor").hover(function() {
$(this)
.addClass('altbgcolor-active')
.find(".sharedp")
.slideDown('slow');
},function () {
$(this)
.removeClass('altbgcolor-active')
.find(".sharedp")
.slideUp('slow');
});
$(".example7").colorbox({
onOpen:function(){ alert('onOpen: colorbox is about to open'); },
onLoad:function(){ alert('onLoad: colorbox has started to load the targeted content'); },
onComplete:function(){ alert('onComplete: colorbox has displayed the loaded content'); },
onCleanup:function(){ alert('onCleanup: colorbox has begun the close process'); },
onClosed:function(){ alert('onClosed: colorbox has completely closed'); }
)};
});
我想将悬停功能和颜色框功能都转换为 jQuery .live() 以便在运行时实时添加的任何新元素,无需刷新页面,都会附加 jQuery。
任何人都可以帮助我吗?我尝试使用下面的代码进行悬停事件,但它似乎只记录了鼠标输入事件
$(".altbgcolor").live('hover',function() {
$(this)
.addClass('altbgcolor-active')
.find(".sharedp")
.slideDown('slow');
},function () {
$(this)
.removeClass('altbgcolor-active')
.find(".sharedp")
.slideUp('slow');
});
请帮忙!
编辑:
使用下面的解决方案,它可以工作,虽然 .addClass('altbgcolor-active') 对于新动态添加的元素,.slideDown('slow') 或 .slideUp('slow') 似乎工作正常工作不正常
$(function() {
$(".altbgcolor").live('mouseenter', function() {
//hover code
$(this)
.addClass('altbgcolor-active')
.find(".sharedp")
.slideDown('slow');
}).live('mouseleave', function() {
//stopped hovering code
$(this)
.removeClass('altbgcolor-active')
.find(".sharedp")
.slideUp('slow');
});
});
有人可以指导一下出了什么问题。您可以在http://mashup2.sunnydsouza.com 看到问题的演示,只需向下滚动页面并单击“更多”按钮即可获取新的动态元素。他们不显示 slideDown()、slideUp() 效果:(
【问题讨论】: