JQuery - 不是第一个

Tom*_*kay 17 javascript jquery

我怎么能说..

On Click .not.first() div
alert('Yeah you clicked a div which is not the first one!');
Run Code Online (Sandbox Code Playgroud)

我的实际代码:

this.$('thumbnails').children().click(function() {

                $('#video').animate({width: 164, height: 20, top: 475, marginLeft: 262},0)

       $('.flv').animate({left: 2222, opacity: '0'},0).css('display', 'none')
        $('.close').animate({opacity: '0'},0)
                clicked = 0

            });
Run Code Online (Sandbox Code Playgroud)

Nic*_*ver 47

有一个:gt()(大于索引)选择器或.slice(1)(@ bobince的偏好:),你的问题按字面翻译将是:

$("div:gt(0)").click(function() {
  alert('Yeah you clicked a div which is not the first one!');
});
//or...
$("div").slice(1).click(function() {
  alert('Yeah you clicked a div which is not the first one!');
});
Run Code Online (Sandbox Code Playgroud)

对于您更新的问题:

$('thumbnails').children(":gt(0)").click(function() {
   $('#video').css({width: 164, height: 20, top: 475, marginLeft: 262});
   $('.flv').css({left: 2222, opacity: '0'}).hide();
   $('.close').css({opacity: '0'});
   clicked = 0;
});
//or...
$('thumbnails').children().slice(1).click(function() {
   $('#video').css({width: 164, height: 20, top: 475, marginLeft: 262});
   $('.flv').css({left: 2222, opacity: '0'}).hide();
   $('.close').css({opacity: '0'});
   clicked = 0;
});
Run Code Online (Sandbox Code Playgroud)

注意使用.css(),如果要进行即时非动画样式更改,请使用此istead.

  • @downvoter - 关注评论?这既正确又短......所以我很好奇. (3认同)
  • 通常避免将非CSS标准选择器(如`:gt`)放在另外标准的表达式中.使用任何非标准功能意味着必须使用Sizzle JS选择器引擎而不是快速本机`querySelectorAll` API来解析整个选择器.通常,最好使用标准选择器和过滤功能.没有`gt()`函数,但`$('div').slice(1)`会做同样的事情. (3认同)
  • @bobince - 虽然我在委托方面没有异议,但你也在创建另一个jQuery对象.更重要的是,我发现这里的观点没有意义,因为`:first`*不是有效的CSS选择器*,它是jQuery特有的. (3认同)

dot*_*tty 8

$(div).not(":first").click(function(){
   alert("Yeah you clicked a div which is not the first one!);
});
Run Code Online (Sandbox Code Playgroud)

请参阅:首先.not来自jquery文档.

$('thumbnails').children().not(":first").click(function() {
    $('#video').animate({width: 164, height: 20, top: 475, marginLeft: 262},0)
    $('.flv').animate({left: 2222, opacity: '0'},0).css('display', 'none')
    $('.close').animate({opacity: '0'},0)
        clicked = 0
});
Run Code Online (Sandbox Code Playgroud)

将是您的问题更新的答案.


Cap*_*tis 5

$("#id").not(':first').click(function(){
alert('Not the first');
});
Run Code Online (Sandbox Code Playgroud)