jQuery如何选择唯一的孩子<a> of a </a> <li> <a> inside a class

时间:2016-02-16 18:59:44

标签: jquery css

I figured out how to do this in css :

.dl-menuwrapper li > a(:only-child)

But I don't manage to make this work in a jQuery selector :

$(document).on('click', '.dl-menuwrapper li a(:only-child)', function () {console.log('it works');});

I don't get what I am missing here and a little help would be much appreciated.

2 个答案:

答案 0 :(得分:2)

您输入了两个额外的括号。

正确的版本:

$(document).on('click', '.dl-menuwrapper li a:only-child', function () {console.log('it works');});

你的css会是这样的:

.dl-menuwrapper li > a:only-child{
 /* some styles */
}

答案 1 :(得分:1)

为什么在()周围使用:only-child。像下面一样删除它。

<强> CSS

.dl-menuwrapper li > a:only-child

<强>的jQuery

$(document).on('click', '.dl-menuwrapper li a:only-child', function () {
    console.log('it works');
});