访问 .each() 上的当前项目
我有以下代码:
HTML
<div id="step-holder">
<div class="step-no">1</div>
<div class="step-dark-left"><a href="#">Basic details</a></div>
<div class="step-dark-right"> </div>
<div class="step-no-off">2</div>
<div class="step-light-left"><a href="#">SEO</a></div>
<div class="step-light-right"> </div>
<div class="step-no-off">3</div>
<div class="step-light-left"><a href="#">Preview</a></div>
<div class="step-light-round"> </div>
<div class="clear"></div>
</div>
JQUERY
$('#step-holder a').click(function (e) {
e.preventDefault();
$(this).parent('.step-light-left').each(function () {
$(this).removeClass('.step-light-left').addClass('.step-dark-left');
});
});
但第二个 $(this)
(在 .each
内)仍然引用原始的 $(this)
>。 所以,它不起作用。
如何才能实现这一目标?我不明白为什么第二个 $(this)
不引用 .each()
当前项目。
或者,如果您有另一种解决方案,可以将所有 step-light-left
更改为 step-light-right
,反之亦然。
我需要做的是关闭所有“未点击”的 div 并打开点击的 div。
i have the following code:
HTML
<div id="step-holder">
<div class="step-no">1</div>
<div class="step-dark-left"><a href="#">Basic details</a></div>
<div class="step-dark-right"> </div>
<div class="step-no-off">2</div>
<div class="step-light-left"><a href="#">SEO</a></div>
<div class="step-light-right"> </div>
<div class="step-no-off">3</div>
<div class="step-light-left"><a href="#">Preview</a></div>
<div class="step-light-round"> </div>
<div class="clear"></div>
</div>
JQUERY
$('#step-holder a').click(function (e) {
e.preventDefault();
$(this).parent('.step-light-left').each(function () {
$(this).removeClass('.step-light-left').addClass('.step-dark-left');
});
});
but the second $(this)
(inside the .each
) still reffers to the original $(this)
.
so, it does not work.
how can this be acheieved ? i dont understand why the second $(this)
does not refer to the .each()
current item.
or maybe, if you have another solution to change all step-light-left
to step-light-right
and the other way around.
what i need to do is turn off all the 'non-clicked' divs and turn on the clicked one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用一系列方法完成更新问题中提出的问题。
演示位于 http:// /jsfiddle.net/gaby/zSXnm/3/
You can do what you are asking in the updated question with a single chain of methods..
demo at http://jsfiddle.net/gaby/zSXnm/3/
“#step-holder a”的父级是包含“a”标签的 div,而不是 #step-holder div。我添加了另一个父函数调用:
Parent of "#step-holder a" is the div containing the "a" tag, not the #step-holder div. I've added another parent function call:
解决方案:
solution:
您的问题可能不是由于
this
的范围造成的。这行是错误的:
$(this).removeClass('.step-light-left').addClass('.step-dark-left');
应该是:
$( this).removeClass('step-light-left').addClass('step-dark-left');
没有点。尝试一下。
添加 Chris 评论:您实际上不需要使用 .each(),您可以直接在选择器上添加/删除类。
更多补充:如果你想关闭所有“未点击”的div并打开点击的div,如果你定义一个名为“active”或其他名称的类会怎么样。然后,您只需在单击函数中执行类似的操作:
Your problem might not be due to the scope of
this
.This line is wrong:
$(this).removeClass('.step-light-left').addClass('.step-dark-left');
It should be:
$(this).removeClass('step-light-left').addClass('step-dark-left');
No dots. Try that.
Adding Chris comment: You don't really need to use .each(), you can just add/remove the class directly on the selector.
More addition: If you want to turn off all "non-clicked" divs and turn on clicked one, what if you define a single class called "active" or something. Then, you just do something like this inside your click function: