简体   繁体   English

如何选择具有相同类的div元素并对单击的元素做些什么

[英]How to select div element with the same class and do something on clicked element

I am here struggling to find a solution for selecting div with similar class in order to remove some class on current (clicked) element. 我在这里努力寻找一种选择具有相似类的div的解决方案,以便删除当前(单击)元素上的某些类。

Here is example of my work. 这是我工作的例子。 You will se Twitter Bootstrap collapse feature where I am trying to remove class fa-angle-up from panel-title when user click on another item. 您将拥有Twitter Bootstrap折叠功能,当用户单击其他项目时,该功能将尝试从panel-title删除类fa-angle-up This way it will change arrow direction when user click on another item. 这样,当用户单击另一个项目时,它将更改箭头方向。

http://quadrifoglio-test.esy.es/sample/ http://quadrifoglio-test.esy.es/sample/

ps. ps。 I believe I need some vent listener but I don?t know which one and how to use it. 我相信我需要一些发泄侦听器,但我不知道该听哪一个以及如何使用它。 Thanks! 谢谢!

here is html structure with jquery which I am using already. 这是我已经在使用的带有jquery的html结构。

     <div class="panel-group" id="accordion">
     <!-- Accordition -->
     <div class="panel panel-primary">
        <div class="panel-heading transition">
           <a data-toggle="collapse" data-parent="#accordion"
              href="#accordionOne">
              <h4 class="panel-title fa fa-angle-up">
                 <span class="month">oct</span>
                 <span>Hrvatski skup poduzetnika</span>
              </h4>
           </a>
        </div>
        <div id="accordionOne" class="panel-collapse collapse in">
           <div class="panel-body">
              <div class="main-event">
                 <a href="#">
                    <span>Main event</span> 
                    <img src="img/main_event.jpg">
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
                       sed do eiusmod tempor incididunt ut labore et dolore magna
                       aliqua.
                    </p>
                 </a>
              </div>
              <div class="events">
                 <span>Eventi</span>
                 <ul>
                    <li><a href="#">Skup amerikanaca podrijetlom iz Hrvatske s rodacima </a></li>
                    <li><a href="#">Branko ima nesto reci, policija zaustavlja vozace. Sta reci.</a></li>
                 </ul>
              </div>
           </div>
        </div>
     </div>

    <!-- jquery  -->

        $( ".panel-title" ).click(function() {
      $( this ).toggleClass( "fa-angle-up" );
    });

    $( ".panel-title.fa.fa-angle-up" ).click(function() {
      $( this ).toggleClass( "fa-angle-down" );
    });

Your page does not work because of the following errors: 由于以下错误,您的页面无法正常工作:

Uncaught SyntaxError: Unexpected token < 403.php:2
Uncaught Error: Bootstrap's JavaScript requires jQuery bootstrap.min.js:11
Uncaught ReferenceError: $ is not defined 

You need to include the necessary scripts (like jQuery). 您需要包括必要的脚本(如jQuery)。


Update 更新资料

You are binding the caret icon to click listeners on the panel which is unreliable. 您将插入符号图标绑定到不可靠的面板上的单击侦听器。 Instead, you want to bind to the bootstrap event like so: 相反,您想像这样绑定到bootstrap事件:

function toggleCaret(e) {
    var $target = $(e.currentTarget),
        $div = $($target.find('.collapse')),
        $theLink = $target.find('a[data-toggle="collapse"]').parent(),
        $links = $('a[data-toggle="collapse"]').parent();

    function doUp() {
        $theLink.addClass('fa-angle-up');
        $theLink.removeClass('fa-angle-down');
    }

    function doDown() {
        $links.removeClass('fa-angle-up');
        $links.addClass('fa-angle-down');
    }

    doDown();
    doUp();
}

$('.panel').on('shown.bs.collapse', function (e) {
    toggleCaret(e);
});

FIDDLE 小提琴

Usually, you would wrap your collapsible groups in such a way that the 'shown' and 'hidden' events would affect even the toggle element. 通常,您将可折叠组包装成这样的方式,即“显示”和“隐藏”事件甚至会影响切换元素。

In your case, I guess you could do 就您而言,我想您可以

$('.fa-angle-up').click(function() {
  $(this).removeClass('fa-angle-up');
});

$('.fa-angle-down').click(function() {
  $('.panel-title').removeClass('fa-angle-up');
  $(this).addClass('fa-angle-up');
});

However, this will probably not work unless you make the .panel-title elements be the ones that manage the collapse behavior. 但是,除非使.panel-title元素成为管理折叠行为的元素,否则这可能行不通。 Currently this is done by their parent. 目前,这是由他们的父母完成的。

Edit: It should still be possible to use hidden and shown events. 编辑:应该仍然可以使用隐藏和显示的事件。

$(document).on('hidden', '.collapse', function () {
   var panel=$(this).closest('.panel');
   var paneltitle=$('.panel-title',panel);
   paneltitle.removeClass('fa-angle-up');
});

$(document).on('shown', '.collapse', function () {
   var panel=$(this).closest('.panel');
   var paneltitle=$('.panel-title',panel);
   paneltitle.addClass('fa-angle-up');
});

well you are not removing the classes at all.. at some point you have both fa-angle-down and fa-angle-up on the same element. 好吧,您根本就不会删除这些类。.在某个点上,同一元素上同时具有fa-angle-down fa-angle-up。

according to jQuery docs the only signature that might suit you case is: 根据jQuery文档,唯一适合您的情况的签名是:

$( "#foo" ).toggleClass( className, addOrRemove );

which is equivalent to: 等效于:

if ( addOrRemove ) {
  $( "#foo" ).addClass( className );
} else {
  $( "#foo" ).removeClass( className );
}

i preffer the old-fashion way: 我喜欢过时的方式:

$(".panel-title").click(function() 
{
   if ($(this).hasClass("fa-angle-up"))
   {
      $(this).removeClass("fa-angle-up").addClass("fa-angle-down");
   }
   else
   {
       $(this).removeClass("fa-angle-down").addClass("fa-angle-up");
   }
});

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

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