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

Categories

0 votes
244 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 Answer

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...