jQuery树向上遍历
jQuery Tree Up Traversal
简单的问题。我试图将"I"标签与树中的面板指示器类作为目标。此外,我需要从面板编辑' I '标签中删除'hide'类。这是HTML:
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<i class="panel-indicator glyphicon glyphicon-chevron-down pull-right"></i>
<i class="panel-edit glyphicon glyphicon-pencil pull-right hide"></i>
<h4 class="panel-title" id="-collapsible-group-item-#1-">
<a data-toggle="collapse" data-parent="#collapseOne" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne" class="collapsed">System Details</a>
<a class="anchorjs-link" href="#-collapsible-group-item-#1-"><span class="anchorjs-icon"></span></a>
</h4>
</div>
</div>
下面是脚本代码。虽然它有效,但似乎是一种草率的方法来让它工作。所以我只是想知道如何正确地编写它,以便它将'rotate'类设置为' I'标签,并使用panel-indicator类。
$('.panel-heading > h4.panel-title > a').click(function () {
if (!$(this).hasClass("open")) {
$(this).addClass("open");
$(this).parent().prev('i.panel-indicator').addClass('rotate');
} else if (
$(this).hasClass("open")) {
$(this).removeClass('open');
$(this).parent().prev().prev('i.panel-indicator').removeClass('rotate');
}
});
尝试。最近的相反:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>closest demo</title>
<style>
li {
margin: 3px;
padding: 3px;
background: #EEEEEE;
}
li.hilight {
background: yellow;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<ul>
<li><b>Click me!</b></li>
<li>You can also <b>Click me!</b></li>
</ul>
<script>
$( document ).on( "click", function( event ) {
$( event.target ).closest( "li" ).toggleClass( "hilight" );
});
</script>
</body>
</html>
来源:https://api.jquery.com/closest/
编辑:在你的情况下:
$('.panel-heading > h4.panel-title > a').click(function () {
if (!$(this).hasClass("open")) {
$(this).addClass("open");
$(this).closest('i.panel-indicator').addClass('rotate'); //edited line
} else if (
$(this).hasClass("open")) {
$(this).removeClass('open');
$(this).closest('i.panel-indicator').removeClass('rotate'); // edited line
}
});
$('.panel-heading > h4.panel-title > a').click(function () {
if (!$(this).hasClass("open")) {
$(this).addClass("open");
$(this).closest('.panel-heading').find('i.panel-indicator').addClass('rotate');
$(this).closest('.panel-heading').find('i.panel-edit').removeClass('hide');
} else if (
$(this).hasClass("open")) {
$(this).removeClass('open');
$(this).closest('.panel-heading').find('i.panel-indicator').removeClass('rotate');
$(this).closest('.panel-heading').find('i.panel-edit').addClass('hide');
}
});
相关文章:
- 遍历类元素数组,并在jquery中选择同级元素
- Jquery遍历表元素
- JQuery 遍历当前 SELECT 值
- 当知道同一hiearch中至少有一个元素时,遍历到元素.结构使用jquery
- 循环遍历元素jquery选择器
- 如何使用jquery遍历具有动态添加元素的数组
- 遍历jQuery中的表元素
- 遍历 jQuery:循环遍历子项
- 遍历jQuery JSON对象数组
- 如何缓慢地遍历 jquery 循环,并每次替换占位符文本
- 循环遍历 jQuery 中的对象
- 如何遍历 jQuery 选择器的结果
- JQuery/Javascript 循环遍历 JQuery Mobile 滑块,并根据它们的值创建一个 JSON 字符串
- 如何遍历jQuery元素
- 循环遍历jQuery元素的数组并移除每个'活动'元素
- 如何循环遍历jQuery的所有元素
- 循环遍历jquery中的元素
- 如何循环遍历jquery对象数组和.hide()
- 遍历jquery数据()对象,并将它们推送到另一个数据()的对象(数组)中
- 遍历 jQuery 中表中的第二列