我的jQuery代码不再适合jQuery 1.9 .on。有人告诉我现在该怎么办?

时间:2013-03-13 07:39:17

标签: html css jsfiddle

$(".p-list > ul > li").click(function () {

    if ($(this).children('.sub-parts').length > 0) {

        if (!$(this).hasClass('open')) {
            $(this).addClass('open');
            $(this).find('.sub-parts').fadeIn('normal');
        }
    }
});


$(document.body).on('click', ".p-list .open-tl", function () {
    if ($(this).parent('.open').length > 0) {
        $open = $(this).parent('.open');
        $($open).find('.sub-parts').hide();
        $($open).removeClass('open');
    }
});

我有这个代码在点击时隐藏div并在他们点击open'tab .open-tl时显示它们。

问题是当我不想运行第二个代码时,在Fadein代码之后执行第二个代码。

请有人检查http://jsfiddle.net/vNfeD/

1 个答案:

答案 0 :(得分:2)

$open = $(this).parent('.open');
$($open).find('.sub-parts').hide();
$($open).removeClass('open');

必须是

$open = $(this).parent('.open');
$open.find('.sub-parts').hide();
$open.removeClass('open');

你可以减少它

$(this).parent('.open').removeClass('open').find('.sub-parts').hide();

如果可以删除父项,如果对象为空,则jquery将不执行任何操作

if ($(this).parent('.open').length > 0) { // remove that

当孩子被隐藏时不允许第二个代码执行,添加一个event.stopPropagation();就在没有.open和有子时,检查小提琴

http://jsfiddle.net/r043v/vNfeD/5/