简体   繁体   English

在jQuery中单击选项卡后,是否禁用点击?

[英]Disable clicks when the tab has been clicked in jquery?

I am having this problem. 我有这个问题。 In my code when I repeatedly click on the tab: mod-2 or mod-3, the border will continue to expand. 在我的代码中,当我反复单击选项卡:mod-2或mod-3时,边框将继续扩大。 How to stop when the tab is clicked? 单击选项卡时如何停止?

Problem 2: How do I check if code div class="l-item" already exists in html? 问题2:如何检查html中是否已存在代码div class =“ l-item”? If it already exists, will the .wrapInner in js stop running?: 如果已经存在,js中的.wrapInner会停止运行吗?:

$('.vd-list li').wrapInner('<div class="l-item"></div>');

Demo: http://jsfiddle.net/b9tcvjxg/ 演示: http//jsfiddle.net/b9tcvjxg/

 $('.mod-1').on('click', function() { $('.display-tab .on').removeClass('on'); $(this).addClass('on'); $('.vd-list').removeClass('mod-2').removeClass('mod-3').addClass('mod-1'); $('.vd-list .l-item').contents().unwrap(); $('.vd-list .v-date, .vd-list .v-info-i:last-child, .vd-list .v-desc').show(); }); $('.mod-2').on('click', function() { $('.display-tab .on').removeClass('on'); $(this).addClass('on'); $('.vd-list').removeClass('mod-1').removeClass('mod-3').addClass('mod-2'); $('.vd-list li').wrapInner('<div class="l-item"></div>'); $('.vd-list .v-date, .vd-list .v-info-i:last-child').hide(); $('.vd-list .v-desc').show(); return false; }); $('.mod-3').on('click', function() { $('.display-tab .on').removeClass('on'); $(this).addClass('on'); $('.vd-list').removeClass('mod-1').removeClass('mod-2').addClass('mod-3'); $('.vd-list li').wrapInner('<div class="l-item"></div>'); $('.vd-list .v-date, .vd-list .v-info-i:last-child, .vd-list .v-desc').hide(); }); 
 .tab-list { display: inline-block; vertical-align: middle; } .tab-list li { float: left; height: 48px; line-height: 48px; margin-right: 20px; cursor: pointer; color: #222; text-align: center; font-size: 14px; font-weight: bold; } .mod-2 .l-item, .mod-3 .l-item { border: 3px solid #111; } .mod-1 .r::after { content: "Mod 1"; color: red; font-weight: bold; } .mod-2 .r::after { content: "Mod 2"; color: green; font-weight: bold; } .mod-3 .r::after { content: "Mod 3"; color: blue; font-weight: bold; } ul { list-style: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <ul class="tab-list display-tab"> <li class="mod-1 on">Mod 1</li> <li class="mod-2">Mod 2</li> <li class="mod-3">Mod 3</li> </ul> <ul class="vd-list mod-1"> <li> <div class="l"></div> <div class="r"> <div class="v-desc">desc</div> <div class="v-info"> <span class="v-info-i">1</span> <span class="v-info-i">2</span> <span class="v-info-i">3</span> </div> <div class="v-date">date</div> </div> </li> </ul> 

you could do something like this: 您可以执行以下操作:

$('.mod-2').on('click', function() {
  $('.display-tab .on').removeClass('on');
  $(this).addClass('on');

  $('.vd-list').removeClass('mod-1').removeClass('mod-3').addClass('mod-2');

  var alrdyExists = $(".vd-list").find(".l-item").length > 0;
    if(!alrdyExists) {
       $('.vd-list li').wrapInner('<div class="l-item"></div>');
    }

  $('.vd-list .v-date, .vd-list .v-info-i:last-child').hide();
  $('.vd-list .v-desc').show();
    return false;
});

This will only border mod-2 content once as it checks whether mod-2 already contains l-item by using find and checking the length of the result. 这仅会限制mod-2内容的边界,因为它会通过使用find并检查结果的长度来检查mod-2是否已包含l-item。

You can apply the same method to the mod-3 on click. 您可以对单击的mod-3应用相同的方法。

Here is a working fiddle just in case: http://jsfiddle.net/e3fjqnhb/2/ 这是一个工作提琴,以防万一: http : //jsfiddle.net/e3fjqnhb/2/

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

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