Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
第一个子元素为span的li的jQuery选择器_Jquery_Html_Jquery Selectors - Fatal编程技术网

第一个子元素为span的li的jQuery选择器

第一个子元素为span的li的jQuery选择器,jquery,html,jquery-selectors,Jquery,Html,Jquery Selectors,以下是我无法更改的HTML结构: 我成功地为那些li附加了一个单击事件处理程序,如下所示: $(document).on("click", "#menu li.top-level", function (e) { // whatever code }); 但是,我不希望处理程序捕获对具有z-index:auto的li的单击。我试过这个: $(document).on("click", "#menu li.top-level[style='z-index: auto;']",

以下是我无法更改的HTML结构:

我成功地为那些
li
附加了一个单击事件处理程序,如下所示:

$(document).on("click", "#menu li.top-level", function (e) { 

     // whatever code

});
但是,我不希望处理程序捕获对具有
z-index:auto
li
的单击。我试过这个:

$(document).on("click", "#menu li.top-level[style='z-index: auto;']", function (e) {

但是没有运气


另外,如果您想要的
li
元素的第一个子元素保证是
a
,而您不想要的元素的第一个子元素总是
span
,则我想在处理程序中捕获的
li
具有
,您最好根据该条件进行选择,因为按样式匹配元素通常很脆弱:

$(document).on("click", "#menu li.top-level:has(> a:first-child)", function (e) {

如果您想要的
li
元素的第一个子元素保证为
a
,而您不想要的元素的第一个子元素始终为
span
,则最好根据该条件进行选择,因为按样式匹配元素通常是脆弱的:

$(document).on("click", "#menu li.top-level:has(> a:first-child)", function (e) {

因为您不想选择具有
style='z-index:auto;'然后使用选择器和将锚点作为第一个子元素的目标元素

$(document).on("click", "#menu li.top-level:not([style='z-index: auto;']):has(> a:first-child)", handler);
$(document).on(“click”,“#menu li.top-level:not([style='z-index:auto;')):has(>a:first-child)”,function(){
console.log($(this.text());
});

  • 2

因为您不想选择具有
style='z-index:auto;'然后使用选择器和将锚点作为第一个子元素的目标元素

$(document).on("click", "#menu li.top-level:not([style='z-index: auto;']):has(> a:first-child)", handler);
$(document).on(“click”,“#menu li.top-level:not([style='z-index:auto;')):has(>a:first-child)”,function(){
console.log($(this.text());
});

  • 2

我认为你需要
:not()
“#菜单li.顶层:not([style='z-index:auto;'])”
正如@Satpal所说的-你的标准似乎是颠倒的。我认为你需要
:not()
“#菜单li.顶层:not([style='z-index:auto;'])”
Satpal说了什么-你的标准似乎有点颠倒了。@chiapa:如果你的编辑认为这根本不应该发生,我的答案应该有效。你说得对,我忘记了最后一个括号。Thanks@chiapa当前位置如果你的编辑提示这根本不应该发生,我的答案应该有效。你是对的,我忘记了最后一个括号。谢谢