jQuery 中 click() 和 onclick() 的区别
1,onclick 是绑定事件, 告诉浏览器在鼠标点击时候要做什么
2,click() 方法的主要作用是触发调用 click 方法元素 onclick 事件.[推荐学习: jQuery 教程 https://www.html.cn/jskuangjia/jquery/ ]
3, 当 click() 被点击时, 首先执行的是对应元素的 onclick() 函数, 然后再是 click(), 示例如下:
JS 代码 test.JS:
- $("#testclick").click(function(){
- console.log("$(\"#testclick\").click");
- });
- function test123(){
- console.log("function test()");
- }
- <!DOCTYPE HTML>
- <HTML>
- <head>
- <title>
- TestJS.HTML
- </title>
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="this is my page">
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <!--<link rel="stylesheet" type="text/CSS" href="./styles.css">-->
- </head>
- <body>
- This is my HTML page.
- <br>
- <h1>
- Test HTML
- </h3>
- <div>
- <input type="button" onclick="test123();" class="input1" id="testclick"
- value="test_button" />
- </div>
- <!-- JS-->
- <script src="js/lib/jquery.js">
- </script>
- <script src="js/test.js">
- </script>
- <!-- <script src="js/test2.js"></script> -->
- </body>
- </HTML>
结果:
控制台打印出的顺序是:
- function test()
- $("#testclick").click
更多 web 前端开发 https://www.html.cn/ 知识, 请查阅 HTML 中文网 !!
来源: http://www.css88.com/qa/jquery/16440.html