jquery接下来将找不到下一个div

时间:2017-12-27 20:40:23

标签: jquery

这让我大吃一惊,因为对于我的生活,我似乎无法找到next() div。所以这是我目前的结果

HTML

<div id="divtoshow" style="position: fixed; display:none; border: solid 1px #cccccc; background-color: #FFFFFF; width: 100px">
    <div class="divtoshow-padding divtoshow-hover pointer new_type">New</div>
    <div class="divtoshow-padding divtoshow-hover pointer rename_type">Rename</div>
    <div class="divtoshow-padding divtoshow-hover pointer delete_type">Delete</div>
</div>
<div data-id="testing0">
    <div class="types_list_button pointer" data-id="testing1">
        <i class="fa fa-caret-right" aria-hidden="true"></i>
        <i class="fa fa-folder-o" aria-hidden="true"></i>
        <span class="hover_point" data-lvl="1" data-id="0">All Types</span>
    </div>
    <div class="types_list" style="display: none" data-id="testing2"></div>
</div>

JQUERY

var thisforsure_object;

$(document).on('contextmenu', '.hover_point', function() {
    thisforsure_object = $(this);
})

在后来的jquery

$(document).on('click', '.new_type', function() {
    hoverdiv(event, 'divtoshow');
    thisforsure_action = 'new';
    thisforsure_object.removeClass('hover_point');
    $(thisforsure_object.parent()).after('<input class="type_rename_input" type="text">');
    $('.type_rename_input').focus();
    $(document).on('blur', '.type_rename_input', function() {
        var thisforsure_text_new = $(this).val();
        if ($(document).not('.type_rename_input')) {
            var asset_type_id = thisforsure;
            var asset_type_name = thisforsure_text_new;
            $.ajax({
                type: 'post',
                url: 'dosomething.php',
                data: {asset_type_id: asset_type_id, thisforsure_lvl: thisforsure_lvl, thisforsure_parent: thisforsure_parent, thisforsure_action: thisforsure_action, asset_type_name: thisforsure_text_new},
                success: function (data) {
                    if (data == 'yes') {
                        thisforsure_object.addClass('hover_point');
                        alert(thisforsure_object.closest('.types_list_button').next('.types_list').attr('data-id'));
                        $('.type_rename_input').remove();
                    } else {
                        swal("",data,"error");
                        exit;
                    }
                }
            })
        }
    })
})

我在那里放了data-id所以我可以确保我抓住正确的值并进行测试

// this will alert "testing0"
alert(thisforsure_object.closest('.types_list_button').parent().attr('data-id'));

// this will alert "testing1"
alert(thisforsure_object.closest('.types_list_button').attr('data-id'));

// both of these will alert "undefined" instead of testing2
alert(thisforsure_object.closest('.types_list_button').next().attr('data-id'));
alert(thisforsure_object.closest('.types_list_button').next('.types_list').attr('data-id'));

我错过了什么?我想抓住data-id div next()

.types_list

0 个答案:

没有答案