jQuery 绑定事件:
- // $(".btn").on('click',function () {
- // $(".c1").toggleClass('c2')
- // });
- $(".btn").click(function () {
- $(".c1").toggleClass('c2')
- })
JS 中的绑定事件
1, 在标签中直接添加 onclick=fun() 事件, script 中写函数
- 2,
- <script defer>
- function clickHandler() {
- alert("onclick attribute in html");
- }
- function clickHandler2(e) {
- alert(e.target.innerHTML);
- }
- document.getElementById("jsOnClick").onclick = clickHandler2;
- document.getElementById("addEventListener").addEventListener("click",
- clickHandler2);
- </script>
来源: http://www.bubuko.com/infodetail-2993083.html