search

Home  >  Q&A  >  body text

javascript - How does NetEase Cloud Music realize that the a tag href has a value and does not jump?

For example, this page http://music.163.com/#/song?i...
Click on the song title below, the page does not refresh or jump, how to achieve this.

扔个三星炸死你扔个三星炸死你2778 days ago1072

reply all(5)I'll reply

  • 为情所困

    为情所困2017-07-05 10:58:54

    http://music.163.com/#/song?i...
    #The thing after the number is called a fragment, or it can also be called an anchor point. This thing will not refresh the browser, nor will it submit a request to the server, but it can generate a browser record.
    Get the value after the # number is window.location.hash
    So you only need to monitor the change of this hash value onhashchange

    reply
    0
  • 天蓬老师

    天蓬老师2017-07-05 10:58:54

    window.location.hash

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-07-05 10:58:54

    iframe

    reply
    0
  • 某草草

    某草草2017-07-05 10:58:54

    I guess you are a newbie... Take a look at the source code

    
    _onAnchorClick = function(_event){//截获所有<a>标签的点击事件,自定义页面的跳转
        _event = _event||window.event;
        var _el = _event.target||_event.srcElement,
        _base = location.protocol+'//'+location.host;
        
        while(_el&&_el!=document){
            // ...
        }
    
    }
    
    _addEvent(document,'click',_onAnchorClick);

    In fact, I just used Event.preventDefault

      <a href="https://baidu.com">我想跳转到百度搜索哆啦A梦</a>
    
    
      <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
      
      <script>
        
        $('a').on('click', function (event) {
          event.preventDefault();
        })
      
      </script>

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-07-05 10:58:54

    The page is actually not refreshed. You know "#target", which can jump your page to the target location. This is actually similar to this. Further, you can learn about js hash (mentioned above) and routing

    reply
    0
  • Cancelreply