Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

on('click',func) 和 $(document).on('click','.smt',func)?

自学小白想问一下两者的分别

$('.stm').on('click',function(){
});  
$(document).on('click','.stm',function(){
});  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
$(document).on('click','要选择的元素',function(){})
   on方法包含很多事件,点击,双击等等事件。和$().click()的用法一样,最大的区别即优点> 是如果动态创建的元素在该选择器选中范围内是能触发回调函数。
...动态添加的元素也能被点击触发函数

来源 : jQuery的区别:$().click()和$(document).on('click','要选择的元素',function(){})的不同 - 热爱前端的17号诶 - 博客园


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...