Avoid window jump to top when clicking #-links

后端 未结 12 1179
礼貌的吻别
礼貌的吻别 2020-11-27 21:32

I\'ve got a page with some questions and answers, the answers are collapsed by default. When they click the question I expand the hidden answer-div. The problem is that when

12条回答
  •  感动是毒
    2020-11-27 22:15

    $('body').on('click', '[href^=#]', function (e) { e.preventDefault() });

    if the selector ex.."body" is there during the initial render then use the any selector .. id ... to target the general to have jQuery (as of 1.8.2) iterate over. the "On handler invoke a method called "bind" which is used for newly added content to the DOM",. Using the "[href^=#] will select any href that are in the section tag but you can replace section with anything or nothing and it applies a cancellation to the click event. This technique is great for dynamically created content to the DOM

提交回复
热议问题