Avoid window jump to top when clicking #-links

后端 未结 12 1155
礼貌的吻别
礼貌的吻别 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:01

    Actually, the easiest way to do this is to remove the href attribute from your anchor tag. As of HTML5, anchor tags don't need to include href attributes to be semantic.

    So

    Myquestion
    

    instead of

    Myquestion
    

    This works in IE8+, Chrome, and Firefox. Note that :link css styles won't apply to anchor tags that don't include href attributes.

    If you need the href attribute and/or IE7 compatibility, then

    $("#myID").click(function(e) {
    if(e.preventDefault)
        e.preventDefault();
    else
        e.stop();
    });
    

    is probably the best way to go.

提交回复
热议问题