如何使用:not()选择器,包括parent:first-child元素

时间:2018-12-23 19:21:59

标签: javascript jquery

我有一个JQuery函数,使tr元素成为超链接,但是,当单击td之外的任何子元素时,它不应触发。我也想排除第一个td元素,因为它包含一个复选框。

我尝试了此操作,但是它不允许任何操作,例如复选框,子链接和子javascript:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.view_miss, container, false);

    lv_list = view.findViewById(R.id.lv_list);

    return view;
}

@Override
public void onResume() {
    firebaseShoppingList = new FirebaseShoppingList(context, uid);
    if (!MainApplication.firebaseShoppingLists.contains(firebaseShoppingList)) {
        MainApplication.firebaseShoppingLists.add(firebaseShoppingList);
        firebaseShoppingList.setFirebaseListener();
    } else {
        int index;
        index = MainApplication.firebaseShoppingLists.indexOf(firebaseShoppingList);
        firebaseShoppingList = MainApplication.firebaseShoppingLists.get(index);
    }

    firebaseShoppingList.setFirebaseUpdateDataListener(this);

    updateList();

    super.onResume();
}

然后我尝试了一下,但似乎根本不起作用:

$('table.table-striped tr[data-href] td').click(function (e) {
    e.stopPropagation();
    var $parent = $(this).parents('tr');
    window.location = $parent.data('href');
}).children().click(function(e) {
    return false;
});

以下是表格行的示例:

$(this).on('click', 'table.table-striped tr[data-href] td:not(:first-child):not(a.codelink):not(.actions):not(.dropdown-toggle):not(.dropdown):not(.caret):not(input)', function (e) {
    e.stopPropagation();
    var $parent = $(this).parents('tr');
    window.location = $parent.data('href');
});

1 个答案:

答案 0 :(得分:0)

使用is()来确保它是单击的td元素,以防止该操作在任何子元素上运行:

$('table.table-striped tr[data-href] td').click(function (e) {
    if ($(e.target).is('td'))
    {
        e.stopPropagation();
        var $parent = $(this).parents('tr');
        window.location = $parent.data('href');
    }
});