简体   繁体   中英

jQuery toggle next element from $(this)

I want to create a button with toggles a div element on click. When i click the button nothing happens on my website. I think there is a problem with the $(this) selector, but i cannot guess why. The Code is working on jsfiddle https://jsfiddle.net/06j3kug8/

HTML

<a class="btn btn-primary flip">Spoiler</a>
<div class="spoiler">test</div>

CSS

.spoiler { display:none }

Script in footer

<script type="text/javascript">

jQuery.noConflict();

jQuery(function($){

    $(".flip").on("click",function(){
       $(this).next().slideToggle();
    });


 });
 </script>

I think you call your jquery.js in bottom please take it on top and before your jquery custom code like below:-

Its working fine:-

The Code is working on jsfiddle because of there is already called jquery.js on top & before your custom jquery code.

 jQuery.noConflict(); jQuery(function($){ $(".flip").on("click",function(){ $(this).next().slideToggle(); }); }); 
 .spoiler { display:none } 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a class="btn btn-primary flip">Spoiler</a> <div class="spoiler">test</div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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