Html代码
<a
name="firstAnchor">&nsbp</a>
锚点使用
Html代码
<a
href="#firstAnchor">跳至第一个锚点</a>
但对于js呢,在js中如何在完成一个操作后跳至页面的某个固定锚点呢
js中location.href可以跳转至某个url;
跳至(定位到)某个固定锚点的办法
1、location.href
=
"#firstAnchor"
//
firstAnchor为锚点名称
2、window.location.hash
=
"#firstAnchor"
//
firstAnchor为锚点名称
经测试:hash只会在跳转到此页面的第一次起作用,再次刷新此页面将不起作用,而href始终起作用
以上这篇js
定位到某个锚点的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
你可以随意定位,然后通过连接锁定到位置<a name="top" id="top">定位</a>
<a href="#top">跳走到定位的位置</a>
获取地址栏参数值http://www.xxx.com/index.html?ver=1.0&id=6#page1
getQueryVariable(ver)// 1.0
getQueryVariable(id) // 6
我们可以用javascript获得其中的各个部分
1、整个URl字符串(在浏览器中就是完整的地址栏 http://www.xxx.com/index.html?ver=1.0&id=6#page1 )
本例返回值: ( http://www.xxx.com/index.html?ver=1.0&id=6#page1 )
2、URL 的协议部分 ( http://www.xxx.com/index.html?ver=1.0&id=6#page1 )
本例返回值:http:
3、URL 的主机部分 ( http://www.xxx.com/index.html?ver=1.0&id=6#page1 )
本例返回值: www.xxx.com
4、URL 的端口部分 ( http:192.168.1.152:8080/index.html)
如果采用默认的80端口(update:即使添加了:80),那么返回值并不是默认的80而是空字符
本例返回值:"8080"
5、URL 的路径部分( http://192.168.1.145/community/page/index.html?categoryId=#page3 )
本例返回值:/community/page/index.html
6、查询(参数)部分
除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值
本例返回值:?ver=1.0&id=6
7、锚点 ( http://192.168.1.145/community/page/index.html?categoryId=#page3 )
本例返回值:#page3