duankui3838 2015-10-20 15:12
浏览 41
已采纳

当试图在JS中单击时改变V形方向时,只有1个V形变化。

I'm trying to change the direction of a chevron on a list-group to point inwards when clicked/activated. However with the code below I'm only getting the first chevron to change directions. I am looping the code for the items inside the menu (chevron included) so I need to find a way to have this functionality mirror across each item in the menu.

I realize this is most likely because the js is targeting an ID. However if I change the code to have directionToggle reflect a class...it changes all the arrows simultaneously.

How can I individually do this, given that I am using php to loop the behavior of each menu item?

<?php foreach ($tasks as $number => $name) { ?>
  <?php $is_heading = ends_with($name, ':'); ?>
    <?php if (!$is_heading): ?>
       <li class="list-group-item" tabindex="-1">
         <a class="listgroupwrap" href="#<?= $task_slugs[$number] ?>"></a>
         <span class="step-number-container"> <?= $number + 1 ?></span>
         <span class="btn-check">
            <i class="glyphicon glyphicon-ok"></i>
         </span>
         <span class="step-checkbox"></span>
         <span class="step-name content-blurred"><?= $name ?></span>
         <span class="step-show-hide">
           <span class="btn btn-showhide">
              <i class="glyphicon glyphicon-chevron-right" id="direction-toggle">
              </i>
           </span>
         </span>
       </li>
     <?php else: ?>
      <li class="list-group-item step-heading" tabindex="-1">
        <a class="listgroupwrap" href="#<?= $task_slugs[$number] ?>">
        </a>
        <span class="step-number-container"> <?= $number + 1 ?></span>
        <span class="step-name content-blurred"><?= $name ?>
        </span>
      </li>

      <?php endif ?>
<?php } ?>

var rightChevron = $('.glyphicon-chevron-right');
    var directionToggle = $('#direction-toggle');

    var fullscreenButton = $('.btn-showhide');
    fullscreenButton.click(function () {
        $('body').toggleClass('fullscreen');
        directionToggle.addClass('glyphicon-chevron-left');
        directionToggle.toggleClass('glyphicon-chevron-right');
    });

</div>
  • 写回答

2条回答 默认 最新

  • douyi4912 2015-10-20 15:27
    关注

    If you're trying to style more than one direction toggle, you need to make it a class. As ID, JQuery is only getting the first one. Once it's a class, use Jquery's .each() function:

    $('.direction-toggle').each(function(){
        $(this).addClass(//class);
    });
    

    Inside your click event.

    EDIT: Per the comments, it seems like you want to change the direction of 1 arrow at at time, whichever one is clicked. Here's what you'll want to do:

    $('.btn-showhide').click(function(){
        $(this).children().addClass(//class).
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?