简体   繁体   中英

jquery click function is not working

The below code is working when i paste it in the browser directly (chrome console). But it is not working from my source file

<script type="text/javascript" >
        $(".test").click(function(){
            $(this).parent().find("div").toggle();
        });
    </script>

Try running it only after the DOM is ready:

$(function(){

  $(".test").on("click", function(){
    $(this).parent().find("div").toggle();
  });

});

try replace $ by writing jQuery..

like,

jQuery(function(){

make sure you wrap in the document.ready function. it ensure that it will bind the function when the page load completes. Document.ready()

$(document).ready(function(){

  $(".test").on("click", function(){
    $(this).parent().find("div").toggle();
  });

});

Always do this while using jquery

$('document').ready(function(){
     $(function(){
        $(".test").on("click", function(){
        $(this).parent().find("div").toggle();
        });
     });
});

Always put code in the function this will make it work with and with out document ready.

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