问题描述
我想选择 .favdelParentFold 子项中的所有列表项,但确保列表项没有< ul>
。如果列表项有任何嵌套子项< ul>
我不想触发它。
I want to select all list items in child of ".favdelParentFold" but make sure list items not have any nested child of <ul>
. If list items have any nested child "<ul>
" I don't want to trigger it.
HTML
<li class="favdelParentFold">
<a href=""><span class="caret-right"></span></a>
<span class="folder"></span><span>fold2</span><span class="trashcan">press me</span>
<ul>
<li><a href="">One 1</a><span class="trashcan">press me</span></li>
<li><a href="">Two 2</a><span class="trashcan">press me</span></li>
<li><a href="">Three 3</a><span class="trashcan">press me</span></li>
<li>
<a href="">Four 4</a><span class="trashcan">press me</span>
<ul>
<li>four 1.1</li>
<li>four 1.2</li>
</ul>
</li>
</ul>
</li>
JS文件:
$(document).ready(function () {
$(".favdelParentFold ~ ul li.trashcan").click(function () {
var tr = $(this).prev('span').text();
var pa = $(this).closet().text();
alert(tr);
alert(pa);
});
});
EX:
如果我点击一个1,两个2,三个3我可以得到这个值和父值,但四个4有孩子< ul>
我想避免触发。
If I click on one 1, two 2, three 3 I can get the this value and parent value but four 4 have child of <ul>
I want to avoid triggers.
预期o / p:
如果点击 我需要一个1和fold2
If click "one 1" i need to get one 1 and fold2
如果点击一个2,我需要得到一个2和fold2
If click "one 2" i need to get one 2 and fold2
如果点击一3,我需要一个3和fold2
If click "one 3" i need to get one 3 and fold2
但是我们不触发四4
推荐答案
我想你正在寻找像下面这样的东西。
I think you are looking for something like following.
$(".favdelParentFold ul li .ecm-trashcan").click(function() {
var tr = $(this).prev().text();
var ma = $(this).closest('.favdelSubFold').find('#sfDel').text();
var pa = $(this).closest('.favdelParentFold').find('#pfDel').text();
alert(tr);
if(tr!=ma) alert(ma);
alert(pa);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="favdelParentFold">
<a href=""><span class="caret-right"></span></a>
<span class="folder"></span><span>fold2</span><span class="trashcan">press me</span>
<ul>
<li><a href="">One 1</a><span class="trashcan">press me</span></li>
<li><a href="">Two 2</a><span class="trashcan">press me</span></li>
<li><a href="">Three 3</a><span class="trashcan">press me</span></li>
<li>
<a href="">Four 4</a><span class="trashcan">press me</span>
<ul>
<li>four 1.1</li>
<li>four 1.2</li>
</ul>
</li>
</ul>
</li>
更新:根据小提琴html js
将如下所示:
Update: according to fiddle html the js
will be like this:
$(".favdelParentFold ul li .ecm-trashcan").click(function () {
if ($(this).parent().find('ul').length || $(this).closest('.favdelSubFold').length) return;
var tr = $(this).prev('a').text();
var pa = $(this).closest('.favdelParentFold').find('#pfDel').text();
alert(tr);
alert(pa);
});
这篇关于如何选择所有列表项,但列表项不具有< ul>或< ol>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!