Keep URL unaffected when anchor link is clicked

前端 未结 5 1976
忘了有多久
忘了有多久 2021-02-04 12:26

I\'ve checked other posts on here, no results of what I\'m looking for. I want to click on

About
Conte
相关标签:
5条回答
  • 2021-02-04 12:29

    You can do what you want using javascript and jquery, example below (note that this is using an old version of jquery):

    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
    
        <script type='text/javascript'>
        jQuery(document).ready(function($) {
    
            $(".scroll").click(function(event){
                event.preventDefault();
                $('html,body').animate({scrollTop:$(this.hash).offset().top}, 1200);
            });
        });
        </script>
    </head>
    <body>
        <a class="scroll" href="#codeword">Blue Words</a>
        <div id="codeword"></div>
    </body>
    </html>
    
    0 讨论(0)
  • 2021-02-04 12:30
    window.location.hash = ""  
    

    is the possible way I could find.

    hash gives the string next to #.

    0 讨论(0)
  • 2021-02-04 12:34

    One possible workaround is to use a <button> instead of a <a>.

    So rather than....

    <a href="#about">About</a>
    <div id="about">Content of this..</div>
    

    ...you can change it to

    <button href="#about">About</button>
    <div id="about">Content of this..</div>
    

    This way the anchor link will not affect the URL.

    0 讨论(0)
  • 2021-02-04 12:53

    Played around with this myself and here is a summary of my learnings on the subject.

    Here's the basic link command:

    <A HREF="#codeword">Blue Words</A>
    

    Here's how you denote where the jump will scroll the page:

    <A NAME="codeword">
    

    Here's what's happening

    The A HREF command is the same as a basic link except the link is to a codeword rather than a URL.

    PLEASE NOTICE there is a # sign in front of the codeword. You need that to denote it is an internal link. Without the # sign, the browser looks for something outside the page named after your codeword.

    Your "codeword" can be just about anything you want. I try my best to keep it short and make it denote what it is jumping to. There might be a limit to the number of letters you can use--but I haven't found it yet.

    The point where the page will jump follows the same general format except you will replace the word HREF with the word NAME.

    PLEASE NOTICE there is no # sign in the NAME command.

    Note! Where you place the NAME target will appear at the top of the screen browser.

    Hope it helps.

    0 讨论(0)
  • 2021-02-04 12:53

    //dont use a, use class

     $(document).ready(function(){
     $(".mouse").on('click', function(event) {
    // Make sure this.hash has a value before overriding default behavior
      if (this.hash !== "") {
      // Prevent default anchor click behavior
      event.preventDefault();
     // Store hash
      var hash = this.hash;
     // Using jQuery's animate() method to add smooth page scroll
      // The optional number (800) specifies the number of milliseconds it takes 
     to scroll to the specified area
      $('html, body').animate({
        scrollTop: $("#section").offset().top
      }, 800, function(){
       // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = "";
      });
    } // End if  }); });
    
    0 讨论(0)
提交回复
热议问题