添加/删除类时 jQuery 单击功能表现得很奇怪
我有两个列表项,单击它们时,应将类从“.off”更改为“.on”。一次只能有一个元素为“.on”,因此当一个元素已打开并且单击另一个元素时,两个元素都应将类从“.off”更改为“.on”,反之亦然。如果单击类为“.on”的列表项,它应该将类更改为“.off”
我遇到的问题是,当单击类为“.on”的列表项时,它仍然运行单击函数,就好像它一样有一个“.off”类
我的 html:
<ul>
<li><a href="about" class="off">ABOUT</a></li>
<li><a href="upload" class="off">SUBMIT</a></li>
</ul>
我的 javascript(在 jQuery 1.7.1 上运行)
$('.off').click(function(event) {
event.preventDefault();
$(".on").addClass("off").removeClass("on");
$(this).addClass("on").removeClass("off");
});
$('.on').click(function(event) {
event.preventDefault();
$(this).addClass("off").removeClass("on");
});
有谁知道这里发生了什么?我的代码有问题还是我在这里遇到了某种错误?
I have two list items that, when clicked, should change classes from '.off' to '.on'. Only one element should be '.on' at a time so when one is already turned on and the other is clicked both elements should change classes from '.off' to '.on' and vice versa. If a list item with a class of '.on' is clicked it should change classes to '.off'
The problem I am having is when a list item with class '.on' is clicked it still runs the click function as if it had a class of '.off'
My html:
<ul>
<li><a href="about" class="off">ABOUT</a></li>
<li><a href="upload" class="off">SUBMIT</a></li>
</ul>
My javascript (running on jQuery 1.7.1)
$('.off').click(function(event) {
event.preventDefault();
$(".on").addClass("off").removeClass("on");
$(this).addClass("on").removeClass("off");
});
$('.on').click(function(event) {
event.preventDefault();
$(this).addClass("off").removeClass("on");
});
Does anyone know what is going on here? Is there something wrong in my code or have I encountered some sort of bug here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您使用
click()
绑定事件的选择器用于选择要添加事件处理程序的元素。运行处理程序时不考虑选择器。您应该寻找更像这样的东西:
您可能希望通过向
ul
或li
添加类/id 来使li
选择器更加明确代码>的。为了进一步混淆事情,你也可以这样做(如果你使用的是 jQuery > 1.7);
这是因为
.on()
函数的工作原理是将事件处理程序附加到所选元素 (document
),并且只会在事件上执行处理程序(函数)如果事件源自的元素与选择器.off
在事件触发时(而不是在绑定时)匹配,则指定(click
)。The selectors you're using to bind the event using
click()
are used to select the elements to add the event handler to. The selector is not considered when the handler is run.You should be looking for something more like this:
You might want to make the
li
selector more explicit by adding a class/id to theul
orli
's.To confuse things further, you could also do this (if you're using jQuery > 1.7);
This is because the
.on()
function works by attaching the event handler to the selected elements (document
), and will only execute the handler (the function) on the event specified (click
) if the element that the event originated from matches the selector.off
at the time the event fired, not at binding time.我建议向不同的选择器添加一个点击句柄,这应该适合你......
I would suggest adding a click handle to a different selector, this should work for you...
问题是 jQuery 处理程序在页面加载时附加,并且无论更改其类如何都保持不变。使用
live('click', handler)on('click', handler) 而不是 click()。编辑:刚刚注意到 .live() 在 jQuery 1.7 中已被弃用。
The problem is that jQuery handlers get attached at page load and remain the same regardless of changing their classes. Use
live('click', handler)on('click', handler) instead of click().Edit: just noticed that .live() is deprecated in jQuery 1.7.
我看到的问题是,您的“on”类在单击事件发生时并未运行,因此您的
$('.on').click
方法永远不会被调用。尝试在更改班级后重新分配您的活动(示例如下):
希望这有帮助,
皮特
The problem as I see it is that your "on" class is not in play at the time of the click event, so your
$('.on').click
method is never being called.Try re-assigning your events after changing classes (example follows) :
Hope this helps,
Pete
单击绑定到元素而不是类。
也许您可以将事件附加到
The click is bound to the element not the class.
Maybe you can attach the events to the