选择器的jQuery问题 [英] jQuery problem with selector

查看:73
本文介绍了选择器的jQuery问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常奇怪的问题.我正在使用jQuery拦截特定的标签点击.

I have a very strange problem. I am using jQuery to intercept a particular tag click.

我有:

<a class="question">lorem ipsum</a>
<a class="question_selected">lorem ipsum</a>
<a class="question">lorem ipsum</a>
<a class="question">lorem ipsum</a>

我的jQuery是:

$("a.question").click(function(){.....});

它应该拦截<a>单击,其中class ="question",但是只要我单击<a class="question_selected">,它也会拦截.

It should intercept the <a> click where class="question" but it is also intercepting whenever I click <a class="question_selected">.

可能是什么问题?

我实际上是在单击更改类.我单击的标签应变为"question_selected",所有其他标签应为"question".这是jQuery:$('a.question_selected').removeClass('question_selected').addClass('question'); $(this).addClass('question_selected').removeClass('question');

I am actually changing the classes on click. The tag that I click should become "question_selected" and all other should be "question". Here is the jQuery: $('a.question_selected').removeClass('question_selected').addClass('question'); $(this).addClass('question_selected').removeClass('question');

推荐答案

从类名称中删除下划线.也许jQuery中有一个关于此的错误.

Remove the underscore from the class name. Perhaps there's a bug in jQuery regarding that.

否则,这不会直接回答您的问题,而是一些建议:

Otherwise, this doesn't directly answer your question, but some suggestions:

首先,我不使用交换类,而是只使用两个类:

First of all, instead of swapping the class, I'd just use two classes:

<a class="question">lorem ipsum</a>
<a class="question selected">lorem ipsum</a>

然后,在您的jQuery中,我将缓存您的问题:

Then, in your jQuery, I'd cache your questions:

$questions = $('a.question');

然后您的jquery可以是:

And then your jquery can be:

$questions.click(function(){
    $questions.filter('.selected').removeClass('selected');
    $(this).addClass('selected');
})

这篇关于选择器的jQuery问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆