wps怎么用js获取链接地址

JavaScript023

wps怎么用js获取链接地址,第1张

可以使用 JavaScript 的 window.location 对象来获取链接地址,该对象包括以下属性:

- hash:返回 URL 中的哈希部分(以 # 开头)

- host:返回 URL 中的主机名和端口号

- hostname:返回 URL 中的主机名

- href:返回整个 URL(包括协议名,域名,端口号,路径,查询字符串等)

- pathname:返回 URL 中的路径

- port:返回 URL 中的端口号

- protocol:返回 URL 中的协议名

- search:返回 URL 中的查询字符串(以 ? 开头)

可以用getAttribute()方法获取。

注:总结了一些getAttribute(),setAttribute()在不同浏览器下兼容性以及如何解决这些问题

body里面有这些内容:

<div id="idHeader" class="class-header" title="kingwell" status="1"></div>

<label id="forUserName" for="userName" title="kingwell" status="1"></label>

下面是script的测试:

var el = document.getElementById("idHeader")

alert(el.getAttribute("id"))

alert(el.id)

IE Firfox->idHeader

alert(el.getAttribute("class"))

//IE6,IE7 ->null IE8,IE9,Firefox ->class-header

alert(el.class)

//IE6,IE7,IE8->报错 IE9,Firefox->undefined

alert(el.getAttribute("className"))

//IE6,IE7->class-header IE8,IE9,Firefox ->undefined

alert(el.className)

//All ->class-header

var elfor = document.getElementById("forUserName")

alert(elfor.getAttribute("for"))

//IE6,IE7->undefined IE8,9,Firefox->forUseName

alert(elfor.for )

//IE6,IE7报错,其它为undefined

alert(elfor.title)

//全部输出kingwell

alert(elfor.status)

//IE6-8 ->1 IE9,Firefox->undefined

alert(elfor.getAttribute("status"))

//全部输出 1

总结:

1:常规属性建议使用 node.XXXX。

2:自定义属性建议使用node.getAttribute("XXXX")。

3:当获取的目标是JS里的关键字时建议使用node.getAttribute("XXX"),如label中的for。

4:当获取的目标是保留字,如:class,请使用className代替。

1、通过js代码根据标签名a得到所有超链接标签的JavaScript对象

2、遍历存储超链接对象的数组

3、取出每一个对象,取出其href属性即可

<a href="index.html">首页</a>

<a href="detail.html">商品信息</a>

<a href="cart.html">购物车</a>

<script type="text/javascript">

    var items=document.getElementsByTagName("a")

    for(var i=0i<items.lengthi++){

        var obj=items[i]

        alert(obj.href)

    }

</script>