jquery里怎么添加点击事件

如题所述

    直接使用click事件是不起作用的,我平常使用的两种方法

    1、on事件

    var html1='';

    html1 +=`<div>

    <button type="button" class="btn btn-primary sure btn-mian" onclick="sure()">确认提交</button>

    <button type="button" class="btn btn-default " data-dismiss="modal">暂不提交</button>

    </div>`

    $('.modal-footer').append(html1);

    $('div').on('click','.sure',function(){console.log("+++");});

    2、onclick事件

    var html1='';

    html1 +=`<div>

    <button type="button" class="btn btn-primary sure btn-mian" onclick="sure()">确认提交</button>

    <button type="button" class="btn btn-default " data-dismiss="modal">暂不提交</button>

    </div>`

    $('.modal-footer').append(html1);

    functionsure(){console.logO("===");}

需要获取到div这个元素,可以通过id,class等等方式得到,比如说div的id为"div1",那么就可以这么写了。$('#div1').click(function(){//这里面就是click事件的内容了});

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-09-09
$("#xxx").click(function(){});
$("#xxx").bind("click",function(){})
$("#xxx").on("click",function(){})
$("body").delegate("#xxx","click",function(){})本回答被提问者采纳
第2个回答  2016-09-09
设input的id="a"

$("#a").click(){
function(xxx){}

}
相似回答