Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery只针对所选的类_Jquery - Fatal编程技术网

使用jquery只针对所选的类

使用jquery只针对所选的类,jquery,Jquery,我有以下代码 $('.time-span').hover(function () { $('.leftRadius,.rightRadius').addClass('hovered'); }, function () { $('.leftRadius,.rightRadius').removeClass('hovered'); }); 我的标记如下所示: <DIV class=leftRadius></DIV> <A class=time-spa

我有以下代码

$('.time-span').hover(function () {
    $('.leftRadius,.rightRadius').addClass('hovered');
},
function () {
    $('.leftRadius,.rightRadius').removeClass('hovered'); 
});
我的标记如下所示:

<DIV class=leftRadius></DIV>
<A class=time-span ></A>
<DIV class=rightRadius></DIV>

<DIV class=leftRadius></DIV>
<A class=time-span ></A>
<DIV class=rightRadius></DIV>

<DIV class=leftRadius></DIV>
<A class=time-span ></A>
<DIV class=rightRadius></DIV>

我如何编辑它以只针对一个“.time span”类(我的文档中有多个,我不能使用id)

谢谢

请参见:eq(x)作为css选择器:

例:

请参见作为css选择器的“:eq(x)”:

例:

您也可以在此处使用get(索引),即:

$('.time-span').get(0).hover(function () ...
您也可以在此处使用get(索引),即:

$('.time-span').get(0).hover(function () ...

根据您的标记,我认为您希望为特定部分编写一个悬停

$('.time-span').hover(function () {
    $(this).prev().addClass('hovered');
    $(this).next().addClass('hovered');
},
function () {
    $(this).prev().removeClass('hovered');
    $(this).next().removeClass('hovered');
});
或者简单地说

$('.time-span').hover(function () {
    $(this).prev().toggleClass('hovered');
    $(this).next().toggleClass('hovered');
});

根据您的标记,我认为您希望为特定部分编写一个悬停

$('.time-span').hover(function () {
    $(this).prev().addClass('hovered');
    $(this).next().addClass('hovered');
},
function () {
    $(this).prev().removeClass('hovered');
    $(this).next().removeClass('hovered');
});
或者简单地说

$('.time-span').hover(function () {
    $(this).prev().toggleClass('hovered');
    $(this).next().toggleClass('hovered');
});

当你说just target one.time span“你要瞄准哪一个?该元素与其他元素的区别是什么?当你说just target one.time span“你要瞄准哪一个?该元素与其他元素的区别是什么?工作时比使用直接css选择器慢。工作时比使用直接css选择器慢选择器。您的答案比op的问题更有意义;)+1对于你的鹰眼来说,你的回答比op的问题更有意义;)+为了你的鹰眼