HTML元素的onclick_html/css_WEB-ITnose
## 导语:> 点击事件的几种形式## 问题来源通过JS脚本动态添加HTML代码,HTML代码的标签绑定的点击失效不起作用## 文章说明小弟才疏学浅,网上找了下,总结了常用的HTML标签的点击事件,大概有以下几种:- 直接在HTML标签里面加上onclick事件- 使用Jquery的click方法- 使用Jquery的bind函数绑定click事件- 使用Jquery的live函数绑定click事件- 使用Jquery的on函数绑定click事件- 使用Jquery的delegate函数绑定click事件## 代码- HTML 代码```<div class="container"> <ul> <li onclick="liClick();">onclick方法</li> <li class="click-li">直接click方法</li> <li class="bind-li">bind click方法</li> <li class="on-li">on click方法 </li> <li class="live-li">live click方法 </li> <li class="delegate-li">delegate click方法 </li> <br /> <li class="btn"><button onclick="add_click();">添加onclick</button></li> </ul></div>```- JS代码```<script> function add_click(){ var str ="<br />"; str += "<li onclick=\"liClick();\">onclick方法</li>"; str += "<li class=\"click-li\">直接click方法</li>"; str += "<li class=\"bind-li\">bind click方法</li>"; str += "<li class=\"on-li\">on click方法</li>"; str += "<li class=\"live-li\">live click方法</li>"; str += "<li class=\"delegate-li\">delegate click方法</li>"; $(str).insertAfter(".btn"); }; function liClick(){ alert("onclick方法的li被点击了!"); }; $(function() { $('.click-li').click(function(){ alert("直接click方法的li被点击了!"); }); $('.bind-li').bind('click',function(){ alert("bind click方法的li被点击了!"); }); $("ul").on("click",".on-li",function(){ alert("on click方法的li被点击了!"); }); $('.live-li').live('click',function(){ alert("live click方法的li被点击了!"); }); $("ul").delegate(".delegate-li", "click", function () { alert("delegate click方法的li被点击了!"); }); });</script>```##效果## 结果说明- bind()函数只能针对已经存在的元素进行事件的设置;但是live(),delegate(),on()均支持未来新添加元素的事件设置- bind()函数在1.7版本出来之后,已经不推荐bind(),替代函数为on()。同样,可以用来代替live()函数,但live()函数在1.9版本已经删除;- live()函数和delegate()函数两者类似,但是live()函数在执行速度,灵活性和CSS选择器支持方面较delegate()差些,想了解具体情况,请戳这:http://kb.cnblogs.com/page/94469/</p>- bind()支持Jquery所有版本;live()支持jquery1.8-;delegate()支持jquery1.4.2+;on()支持jquery1.7+;```问题:使用jQuery的live()方法绑定事件,有时会出现重复绑定的情况,如,当点击一个按钮时,此按钮所绑定的事件会并执行n遍。 解决:使用die()方法,在live()方法绑定前,将此元素上的前面被绑定的事件统统解除,然后再通过live()方法绑定新的事件。$("#selectAll").die().live("click",function(){ //事件运行代码 }); ```## 源码源码下载:http://pan.baidu.com/s/1gdZ6UlX

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.
