为什么这段 jQuery 代码只适用于一次迭代?
基本上我有一个下拉图标,它应该展开和折叠其下方的区域。它会更改背景位置并指向下方并显示一些内容。再次单击时,它会折叠内容并更改背景位置。下面是我写的,但它不能正常工作。
$(function() {
$(".aclass").live('click' , function() {
$(this).css("background-position","-156px -42px").addClass("new");
$(".blockedDiv").hide();
$(".mystery").css("max-height","189px");
});
$(".new").live('click', function() {
$(this).css("background-position","0px 0px");
$(".blockedDiv").show();
$(".mystery").css("max-height","1007px");
});
});
我的两个问题: 1:为什么这只适用于一次迭代 2:有没有更好的方法来实现这一点?
Basically I have a drop icon, which should expand and collpase an area beneath it. It changes the background position and points downwards and shows some content.On click again it collapses the content and changes the background position. below is what i have written but it doesn't work properly.
$(function() {
$(".aclass").live('click' , function() {
$(this).css("background-position","-156px -42px").addClass("new");
$(".blockedDiv").hide();
$(".mystery").css("max-height","189px");
});
$(".new").live('click', function() {
$(this).css("background-position","0px 0px");
$(".blockedDiv").show();
$(".mystery").css("max-height","1007px");
});
});
My two questions:
1: Why is this working only for one Iteration
2: Is there a better way to achieve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不会删除添加的
new
类。因此,当您再次单击它时,第二个事件处理程序也会触发,从而反转第一个事件处理程序的操作。作为旁注斜杠提示,您想要的是指定显式 CSS 规则,您可以使用 jQuery 进行切换,这样您就不必搞砸(相对)复杂的逻辑。
然后在你的 jQuery 中:
You're not removing the
new
class you added. Because of that, when you click on it again, the second event handler fires as well, reversing what the first event handler did.As a side note-slash-tip, what you want is to specify explicit CSS rules, which you can toggle with jQuery, so that you don't have to muck through (relatively) complicated logic.
then in your jQuery:
理查德已经指出了您的代码的问题。您需要切换
new
类。更简洁的方法可能是使用
toggleClass()
jQuery 函数。你定义了两个 CSS 类:
和 JS:
在你的 HTML 中,有类似的内容:
Richard has already pointed out the problem with your code already. You need to toggle the
new
class.A cleaner way to this is probably using the
toggleClass()
jQuery function.You define two CSS classes:
And JS:
And in your HTML, have something like:
这是一种稍微不同的方法,应该可行。
编辑:正如 @Richard Neil Ilagan 提到的,你(和我)忘记删除
new
类。Here is a slightly different way of doing it that should work.
EDIT: As @Richard Neil Ilagan mentioned, you (and I) forgot to remove the
new
class.它工作得很好。 jsFiddle
点击“.aclass”,它将:
1)设置'this'的背景位置
2)添加“new”类(这使其成为两个处理程序的目标)
3)隐藏不同的东西.blockedDiv
4)设置.mystery的最大高度
点击“.new”,它将:
1)设置'this'的背景位置
2)显示不同的东西.blockedDiv
3) 设置 .mystery 的最大高度
你打算让它做什么?也许您想删除第一个处理程序中的 .aclass 类,并在第二个处理程序中执行相反的操作(添加 .aclass 删除 .new)?
It's working just fine. jsFiddle
Click ".aclass" and it will:
1) set the background-position of 'this'
2) add the class "new" (which makes it a target for BOTH handlers now)
3) hide a different thing .blockedDiv
4) set the max-height of .mystery
Click ".new" and it will:
1) set the background-position of 'this'
2) show a different thing .blockedDiv
3) set the max-height of .mystery
What did you intend for it to do? Maybe you wanted to remove the class .aclass int he first handler, and do the opposite in the second (add .aclass remove .new)?