【发布时间】:2011-01-06 20:28:36
【问题描述】:
我想弄清楚为什么我的代码不起作用,我快疯了。
我正在设置一个带有活动类的 li 元素,它可以工作..
然后我想选择具有“活动”类的 li 元素 - 并给它一个单击功能,将另一个 div 滑动到视图中,该 div 也由“活动”类标识
这是我的代码:
$(".contentPanel").click(function() {
$(this).siblings().animate({height: minHeight,},500).removeClass("active");
$(this).animate({height: maxHeight,},500).addClass("active");
$('li').removeClass("active");
$(this).children('ul').children('li').addClass("active");
$('.infoPanelSlider').removeClass("active");
$(this).children('.infoPanelWindow').children('.infoPanelSlider').addClass("active");
});
以上工作,活动“contentPanelSlider”内的 li 元素被赋予活动类。
下一点是我卡住的地方:
我想写这个:
$('li.active').click(function()....做点什么
但“活动部分”似乎不起作用 - 如果我只针对“li”,没有活动类选择器,它就可以工作......
我想知道这是否与活动类是由 JQuery 动态添加的事实有关,而不是在样式表中硬编码 - 但这是一个完整的猜测..
如果可以的话请帮忙,我不介意被喷……
【问题讨论】:
-
附带说明,对象中的尾随逗号:
{height: minHeight,}将在 IE 中中断并出现语法错误。删除它... -
另外 - 与其调用 jQuery 4 次来解析
$(this),不如执行var $this = $(this);之类的操作,然后使用$this进行进一步操作