javascript - jQuery Tree Up Traversal -
simple question. i'm trying target 'i' tag panel-indicator class in tree. additionally need remove 'hide' class panel-edit 'i' tag. here's 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>
here's script code. though works, seems sloppy way work. i'm wanting know how write correctly sets 'rotate' class 'i' tag panel-indicator class.
$('.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'); } });
try '.closest' instead:
<!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 <b>click me!</b></li> </ul> <script> $( document ).on( "click", function( event ) { $( event.target ).closest( "li" ).toggleclass( "hilight" ); }); </script> </body> </html>
source: https://api.jquery.com/closest/
edit: in case:
$('.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 } });
Comments
Post a Comment