点击事件没有被触发 - jQuery

时间:2017-10-29 16:10:05

标签: jquery

$(document).on('click', 'a', function(){
    e.preventDefault();
    alert(yes);
});

<a href="#" class="button is-primary" data-toggle="modal" data-id="modal-create-news" style="background-color: #262331 !important;"><i class="fa fa-pencil"></i>&nbsp; Create Announcement</a>

点击按钮后,页面只刷新,因此点击事件未被触发,不确定原因。

1 个答案:

答案 0 :(得分:0)

你需要添加你的Jquery CDN,你还需要在函数中声明局部变量'e'。即使是,也没有声明为变量。我想这是测试警报。所以我把它作为一个字符串。

$(document).on('click', 'a', function(e){
    e.preventDefault();
    alert("yes");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class="button is-primary" data-toggle="modal" data-id="modal-create-news" style="background-color: #262331 !important;"><i class="fa fa-pencil"></i>&nbsp; Create Announcement</a>

相关问题