Jquery click event doesn't work

2019-03-01 19:15发布

问题:

no result occur when i click test1 of test2

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

    <script type="text/javascript">
    $(".trigger").click(function(event){
        console.log("d");
    });
    </script>
</head>
<body>
    <a href="#" class="trigger">Test1</a>
    <a href="javascript:void(0)" class="trigger" >Test2</a>
</body>
</html>

回答1:

You need to put the event registration in document.ready() so that the element you are trying to bind the event are ready to your script.

$(document).ready(function(){

  $(".trigger").click(function(event){
        console.log("d");
    });

});


标签: click jquery