Programmatically scroll to an Anchor Tag

前端 未结 8 1530
孤独总比滥情好
孤独总比滥情好 2020-11-28 07:21

Consider the following code:

GoTo Label2
... [content here] ...
More content


        
相关标签:
8条回答
  • 2020-11-28 07:21

    you can just open the new URL with the name appended, for instance http://www.example.com/mypage.htm#label2

    In JavaScript,

    location.href = location.href + '#label2';
    
    0 讨论(0)
  • 2020-11-28 07:30

    Moving to a anchor from server side, example is c#.

    ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#form';", true);
    
    0 讨论(0)
  • 2020-11-28 07:36

    If the element is an anchor tag, you should be able to do:

    document.getElementsByName('label2')[0].focus();
    
    0 讨论(0)
  • 2020-11-28 07:37

    This JS has generally worked well for me if you also put an ID on the element:

    document.getElementById('MyID').scrollIntoView(true);
    

    This is good as it will also position scrollable divs etc so that the content is visible.

    0 讨论(0)
  • 2020-11-28 07:41

    Using javascript:

    window.location.href = '#label2';
    

    If you need to do it from the server/code behind, you can just emit this Javascript and register it as a startup script for that page.

    0 讨论(0)
  • 2020-11-28 07:43

    I suppose this will work:

    window.location="<yourCurrentUri>#label2";
    
    0 讨论(0)
提交回复
热议问题