bind、delegate、on的区别
生活随笔
收集整理的這篇文章主要介紹了
bind、delegate、on的区别
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
on(type,[data],fn)
on有三個(gè)參數(shù),type代表事件類型,可以為“click"、"onchange"、"mouseover"
data可以不傳,如果傳入data,可以在fn中以e.data取到
fn:函數(shù),形參傳入e,e.target可以拿到當(dāng)前被點(diǎn)擊的元素
delegate(selector,type,[data],fn)
selector:選擇器
on(type,selector,[data],fn)
?
<!DOCTYPE html> <html><head><meta charset="utf-8"><title>test2</title></head><body><ul id="ul" style="padding:20px"><li>1</li><li>2</li><li>3</li><li>4</li></ul><input type="button" value="add" id='add'><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><script type="text/javascript">$(function(){// $("#ul").bind("click",{name:"Annie"},function(e){// console.log(e.target);// console.log(e.data);// }) $("#ul").delegate("li","click",{name:"Annie"},function(e){console.log(e.target);console.log(e.data);})// $("#ul").on("click","li",{name:"Annie"},function(e){// console.log(e.target);// console.log(e.currentTarget);// console.log(e.data);// }) $("#add").click(function(){console.log('add');$("#ul").append("<li>5</li><li>6</li><li>7</li>");})})</script></body> </html>?
轉(zhuǎn)載于:https://www.cnblogs.com/AnnieShen/p/7682059.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的bind、delegate、on的区别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: FPGA时序约束和timequest t
- 下一篇: 新建/克隆虚拟机(配置)