js&jquery(写法对比): click event(点击事件)

发布时间 2023-09-12 15:10:54作者: katesharing

1. js 写法

  //js写法
    document.getElementById("btn1").addEventListener("click", function () {
        alert("This is Line1. \n Note:this is test");
    })
    document.getElementById("btn2").addEventListener("click", function () {
        alert("This is Line1. </br> Note:this is test")
    })

2. jquery写法

 $("#btn1").click(function () {
        alert("This is Line1. \n Note:this is test")
    });
    //$("#btn2").click(function () {
    //    alert("This is Line1. </br> Note:this is test")
    //})
    $(document).on("click", "#btn2", function () {
        alert("This is Line1. </br> Note:this is test")
    })