javascript - 网易云音乐是如何实现a标签href有值不跳转的?
问题描述
比如这个页面http://music.163.com/#/song?i...点击下面的歌名,页面没有刷新也没跳转,是如何实现的。
问题解答
回答1:http://music.163.com/#/song?i...#号后面的东西叫片段,也可以叫锚点。 这东西不会刷新浏览器,也不会提交一个请求给服务器,但是可以生成一条浏览器记录。获取#号后面的值是window.location.hash所以只需要监控这个hash值的变化就可以了onhashchange
回答2:window.location.hash
回答3:iframe
回答4:估计你是新手吧... 多看源代码吖
_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);
其实就是用了 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>回答5:
页面其实就是没有刷新,你知道'#target',可以将你的页面跳转到target的位置,这个其实和这个差不多,进一步你可以了解js的hash(楼上说的),还有路由
相关文章:
1. html5 - H5 如何进行缓存页面 资源包 索引?2. python - scrapy-redis爬虫运行是连接redis数据库连接错误3. html5 - CSS画五角星画的不正4. python - 如何将文本文件的CR换行符换成LF换行符?5. js怎样实现在类似qq的联系人分组中进行搜索后,又可恢复原分组的界面?6. javascript - 这种上传图片预览怎么做?7. python相关问题求解决,有偿8. javascript - input输入框触发change事件后,外部元素会被选中9. 怎样用python给mp3添加封面?10. python3.x - mongodb 显示不出中文,都是方块,该如何解决?