简体   繁体   English

jQuery:此类的addClass和click函数

[英]jQuery: addClass and click function for this class

I have 2 click functions set up, the first one allows you to click and scroll through the .test divs. 我设置了2个click函数,第一个允许您单击并滚动.test div。 After this the .test:last-child is targeted to remove the red class, add the blue class and then fire a click function on the blue class div, and hide it. 之后, .test:last-child的目标是删除red类,添加blue类,然后在blue类div上触发click函数,然后将其隐藏。 Only problem is it doesn't seem to recognise the click function on the .blue div and isn't working. 唯一的问题是,它似乎无法识别.blue div上的单击功能,并且无法正常工作。
jsFiddle demo: http://jsfiddle.net/neal_fletcher/adMYV/1/ jsFiddle演示: http : //jsfiddle.net/neal_fletcher/adMYV/1/
HTML: HTML:

<div class="test red"></div>
<div class="test red"></div>
<div class="test red"></div>
<div class="test red"></div>
<div class="test red"></div>

jQuery: jQuery的:

$(document).ready(function () {
    $(".red").click(function () {
        var next;
        next = $(this).nextAll(".test");
        $('html, body').animate({
            scrollTop: next.offset().top
        }, "slow");
        return false;
    });
});


$(document).ready(function () {
    $('.test:last-child').removeClass('red').addClass('blue');
    $('.blue').click(function () {
        $(this).hide();
        return false;
    });
});

Any suggestions would be greatly appreciated! 任何建议将不胜感激!

Try like this: 尝试这样:

http://jsfiddle.net/adMYV/3/ http://jsfiddle.net/adMYV/3/

CODE

$(document).on("click", ".blue", function () {
    $(this).hide();
    return false;
});

You can do the click event on the .test selector. 您可以在.test选择器上执行click事件。 And in the click function event to user hasClass jquery function and act as you want in each case. 并在用户具有hasClass jquery函数的click函数事件中,在每种情况下都可以根据需要进行操作。

EDIT: Like this: 编辑:这样:

$(document).ready(function () {
$(".test").click(function () {
    if($(this).hasClass('red')) {
        var next;
        next = $(this).nextAll(".test");
        $('html, body').animate({
            scrollTop: next.offset().top
        }, "slow");
    } else if($(this).hasClass('blue')) {
        $(this).hide();
    }
    return false;
});
$('.test:last-child').removeClass('red').addClass('blue');

}); });

Take a look http://jsfiddle.net/adMYV/4/ 看看http://jsfiddle.net/adMYV/4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM