简体   繁体   English

如何只选择 <li> 与一个 <ul> ?

[英]How to select only <li> with a <ul>?

I have an <li> that contains another <ul> and <li> . 我有一个<li> ,其中包含另一个<ul><li>

I want to be able to access either <li> and apply different functionality to each. 我希望能够访问任一<li>并为每个应用不同的功能。

I've been going with the approach of: 我一直在尝试以下方法:

Example A: 范例A:

If this <li> contains a <ul> , do this when clicking on the <li> . 如果此<li>包含<ul> ,请在单击<li>时执行此操作。

Example B: 范例B:

If this <li> does not contain a <ul> , do this when clicking on the <li> . 如果此<li>不包含<ul> ,请在单击<li>时执行此操作。

It's not working though, so I'm trying to test the basics. 但是它没有用,所以我正在尝试测试基础知识。

Example A seems to select both <li> 's, even the nested one that does not contain a <ul> . 示例A似乎选择了两个<li> ,甚至选择了一个不包含<ul>的嵌套的。

HTML 的HTML

<ul>
<li>i do have a ul - i should be green
<ul>
<li>i dont have a ul - i should be red</li>            
</ul>
</li>
<li>i dont have a ul - i should be red</li>
<li>i dont have a ul - i should be red</li>
</ul>

jQuery jQuery的

// why does this first condition cause both li's to be green?

$("li:has(ul)").css('background','Green');

// uncomment the following and only one is green

//$("li:not(:has(ul))").css('background','Red');

jsFiddle jsFiddle

http://jsfiddle.net/rwone/H5ZdE/2/ http://jsfiddle.net/rwone/H5ZdE/2/

Edit: 编辑:

Just to clarify, I am wanting to be specifically selective so that I can apply different click functionality to each <li> , so that is why I need to figure out how to select them accurately and not just rely on 'overiding' inherited styles etc. 为了明确起见,我想特别选择一下,以便可以对每个<li>应用不同的单击功能,因此我需要弄清楚如何准确地选择它们,而不仅仅是依靠“覆盖”继承的样式等。 。

// why does this first condition cause both li's to be green?
$("li:has(ul)").css('background','Green');

The above line does NOT cause both li to be green. 上一行不会导致li均为绿色。 It causes the first parent li only to be green. 它使第一个父级li仅变为绿色。 However, because the child li (which does not contain a ul ) does not have a background color of its own, it appears to be green because it is inheriting its parent's background color. 但是,由于子li (不包含ul )没有自己的背景色,因此它看起来是绿色的,因为它继承了其父级的背景色。

The HTML and Javascript you've written conflict. 您编写的HTML和Javascript冲突。 The second <li> is nested inside the first, and because it's styled background green, all child elements are inside that <li> background. 第二个<li>嵌套在第一个<li>内部,由于它的背景样式为绿色,因此所有子元素都位于该<li>背景中。

See here: http://jsfiddle.net/H5ZdE/4/ 看到这里: http : //jsfiddle.net/H5ZdE/4/

The Javascript is not actually styling that second <li> to background green. Javascript实际上不是将第二个<li>为背景绿色的样式。 It's simply inside the <li> whose background is green. 它只是在背景绿色的<li>内部。

For selecting an li that has a nested ul use 用于选择具有嵌套ulli

$('li ul').parent() (this will return the parent li tags of the selected ul tags. You can use li>ul as well) $('li ul').parent() (这将返回所选ul标签的父li标签。您也可以使用li>ul

try this 尝试这个

$("li:has(ul)").css('background', 'green')
$('li ul').css('background', 'red')

http://jsfiddle.net/H5ZdE/6/ http://jsfiddle.net/H5ZdE/6/

Solution: 解:

The following achieves the desired results ie separate functionality for top level and nested <li> 's. 以下实现了预期的结果,即顶层和嵌套<li>的单独功能。

To be honest I don't yet really know how it works, but it works and I added a bit of extra functionality to test things out. 老实说,我还不太清楚它是如何工作的,但是它能工作,因此我添加了一些额外的功能来进行测试。 But no marks needed for code prettiness. 但是代码美化不需要任何标记。

HTML: HTML:

<div id="container">
<ul class="squares">
<li>
<ul class="nested">
<li class="hidden_li" style="height: 20px; width: 50px; background: yellow; display: none; position: relative; z-index: 999999; left: 0px;top: 70px;">
<a href="">test</a>
</li></ul>
</li>
<li>
<ul class="nested">
<li class="hidden_li" style="height: 20px; width: 50px; background: yellow; display: none; position: relative; z-index: 999999; left: 0px;top: 70px;">
<a href="">test</a>
</li></ul>
</li>
<li></li>
<li></li>
// etc
</ul>
</div>

CSS 的CSS

#container {
height: 205px;
overflow-y: auto;
width: 400px;
}

.select {
border: 2px solid red !important;
}

.select_blue {
border: 2px solid blue !important;
}

.squares {
list-style: none outside none;
margin: 0;
padding: 0;
white-space:nowrap;
}

#container ul li {
background: none repeat scroll 0 0 #FFFFFF;
border: 2px solid #CCCCCC;
border-radius: 4px;
cursor: pointer;
display: inline-block;
height: 100px;
margin-right: 25px;
vertical-align: top;
width: 100px;
}

jQuery jQuery的

// just for <li>
$(document).on("click","ul.squares > li:nth-child(1n-7)", function (e) {
$(".hidden_li").hide();    
$(this).find(".hidden_li").fadeIn();
$("ul.squares > li:nth-child(1n-7)").removeClass("select");
$(this).addClass("select");
e.preventDefault();
//alert("i am a li");
});

// just for nested <li>
$(document).on("click","ul.nested > li", function (e) {
e.stopPropagation();
$("ul.nested > li").removeClass("select_blue");
$(this).addClass("select_blue");
e.preventDefault();
alert("i am a nested li");
});

More comments and demo on jsFiddle: 有关jsFiddle的更多评论和演示:

http://jsfiddle.net/rwone/H5ZdE/31/ http://jsfiddle.net/rwone/H5ZdE/31/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM