英文:A链接标记ie下会自动补全href

所属分类: 网页制作 / HTML/Xhtml 阅读数: 691
收藏 0 赞 0 分享

英文:A链接标记ie下会自动补全href.
Whilst working on the Ajax Link Tracker and MapSurface I have come across an inconsistency in how the href attribute is retrieved using DOM Scripting.
The href attribute is different to other element attributes in that the value set can be relative to the context of the page URL. If you set a link with a relative href attribute
<a href="../development/test1.html">test page</a>
The browser will look at the pages current URL and derive an absolute URL for the link.
http://www.glenn.jones.net/development/test1.html
This is the root of the problem, some browsers return the text of the attribute and others return the derived absolute URL. The results also differ by the method you use to retrieve the href attribute. There are three common ways to access an attribute:
linkobj.href; linkobj[‘href’]; linkobj.getAttribute(‘href’);
The linkobj.href and linkobj[‘href’]; methods of accessing the attribute consistently return the derived absolute URL.
Microsoft has tried to address this by problem adding a second parameter to the getAttribute method. The second parameter can be set to 0,1 or 2. If the parameter is set to 2 the method returns the attribute text. Any other setting will return the derived absolute URL.
linkobj.getAttribute(‘href’); linkobj.getAttribute(‘href’,2); Derived
Absolute URL Attribute Text IE linkobj.href; x IE linkobj.getAttribute(‘href’); x IE linkobj.getAttribute(‘href’,2); x Gecko linkobj.href; x Gecko linkobj.getAttribute(‘href’); x Gecko linkobj.getAttribute(‘href’,2); x Opera linkobj.href; x Opera linkobj.getAttribute(‘href’); x Opera linkobj.getAttribute(‘href’,2); x Get attribute test page Test on IE6, Firefox 1.5 and Opera 8.51.
So what should be returned by the getAttribute method? The W3C DOM Level 2 Core specification which sets out the structure of the getAttribute method does not cover this issue. It is not that either approach is wrong or right. On this point the specification is open to interpretation.
As a coder I would like to be able to access both values. The DOM Core specification should be updated to address the problem.
After a really good exchange with Jim in the comments below, I stand corrected. The specification does say the getAttribute should return the attribute value, not the absolute URL. The Microsoft approach is wrong.
For the time being I am using the old school object property method linkobj.href to return derived absolute URLs. It provides the most consistent results across all browsers. URLs of interest
W3C REC DOM Level 2 Core specification for getAttribute
Gecko documentation for getAttribute
Microsoft documentation for getAttribute
As usual just as I was finishing this post I found this bug report on the QuickMode site which discusses the same subject.
getAttribute HREF is always absolute.html
更多精彩内容其他人还在看

超链接的宽度和高度直接设置不起作用的解决方法

这篇文章主要介绍了超链接的宽度和高度直接设置不起作用的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

html下拉菜单提交后保留选中值而不返回默认值

这篇文章主要介绍了html下拉菜单提交后如何保留选中值而不返回默认值,方法虽简单,但比较实用,需要的朋友可以参考下
收藏 0 赞 0 分享

html用style添加属性示例

这篇文章主要介绍了html用style添加属性的写法,比较实用,有需要的朋友可以参考下
收藏 0 赞 0 分享

HTML元素设置焦点的方法

HTML元素设置焦点,大都是采用js的focus方法来进行设置,下面有个示例,大家可以参考下
收藏 0 赞 0 分享

html用title属性实现鼠标悬停显示文字

实现鼠标悬停显示文字,html中使用title属性就可实现显示文字的效果,这个属性还是比较实用的,需要的朋友可以参考下
收藏 0 赞 0 分享

html 用超链接打开新窗口其可控制窗口属性

这篇文章主要介绍了html如何用超链接打开新窗口其可控制窗口属性,主要使用到js的window.open方法,感兴趣的朋友可以看看哦
收藏 0 赞 0 分享

html 可输入下拉菜单的实现方法

可输入下拉菜单,不可思议是不是, 本例通过一些方法实现这个不可能的事情,感兴趣的朋友可以参考下
收藏 0 赞 0 分享

a标签href属性与onclick事件使用实例

a标签主要用来实现页面跳转,可以通过href属性实现,也可以在onclick事件里实现,下面为大家简要介绍下其具体的使用
收藏 0 赞 0 分享

HTML cellpadding与cellspacing属性图文详解

这篇文章主要介绍了HTML cellpadding与cellspacing属性,有个演示图,相信大家看过之后就知道了,需要的朋友可以参考下
收藏 0 赞 0 分享

input输入框中的光标大小显示不一致的解决方法

chrome浏览器对光标高度的设置原则为,当没有内容的时候光标的高度=input的line-height的值,当有内容时,光标从input的顶端到文字的底部
收藏 0 赞 0 分享
查看更多