dte66654 2015-05-13 13:27
浏览 35
已采纳

奇怪点击创建不同的功能甚至点击链接

I have 3 links which are the names of students.On clicking a link first time or odd number time,another div which contains the details of that student appears.On clicking the same link on second time or even number time, I need to hide the student div.Its working perfectly for me using data() event of jquery.

My requirement is if I click first student link,div with the details of first student will come.If I click the second student link,details of second student will come.If I again click on first link,the student div will hide since data() is associated with it(I think so). But I want to make it displayed because,previous click was on the second link.

My html is like this

<a href="" id="24" class="student_link">George</a>

<a href="" id="25" class="student_link">Leo</a>

<a href="" id="26" class="student_link">Kelm</a>

My jquery:

$("#sections ").on('click','.student_link',function(){
       var clicks = $(this).data('clicks');
      //alert(clicks);
      if (clicks) {
         $("#div_students").hide();
         //alert(1); 
      }
      else
      {
         $("#div_students").show();
         //alert(2);
      }

      $(this).data("clicks", !clicks);
      return false;
   });

That is if I click George for first time, the corresponding div of student will appear,If I click Leo without clicking George to hide the div,I need div of Leo should come.If I click George again,I need div with details of George should come.But for me the div is hiding since the variable click is already true for George.I need the div to be displayed if previous click was not on the current link.

  • 写回答

3条回答 默认 最新

  • dongyuan1870 2015-05-13 13:29
    关注

    You can use the following code:

    $("#sections").on('click', '.student_link', function () {
        var prevClickedId = $('#sections').data('prevId');
    
        if (prevClickedId != $(this).attr('id')) {
            // This was not the previous clicked element
        } else {
            // This was the previous clicked element
        }
    
        $('#sections').data('prevId', $(this).attr('id'));
        return false;
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line