与所有其他浏览器相比,在IE7中错误提取.attr("href")? [英] Wrong extraction of .attr("href") in IE7 vs all other browsers?
问题描述
与其他所有浏览器相比,在IE7中处理链接的attr("href")
命令是否真的不同吗?
假设我在 http://example.com/page.html 上有一个页面,拥有以下HTML:
<a href="#someAnchor" class="lnkTest">Link text</a>
和这个jQuery:
var strHref = $(".lnkTest").attr("href");
然后在IE7中,变量strHref
的值将为"http://example.com/page.htm#someAnchor"
,但在其他浏览器中,变量值将为"#someAnchor"
.
我认为最后提到的案例是最正确的案例,那么这仅仅是IE7是个坏孩子还是jQuery中的错误?
肯定不是jQuery中的错误,而是浏览器对.getAttribute('href')
的不一致实现-我建议仅将.get(0).href
用于一致性.
如果您不想使用绝对URI,似乎可以使用.get(0).getAttribute('href', 2)
访问IE和Mozilla中的属性文本.请注意,但是这在Opera中不起作用,并且我还没有在Safari/Chrome/其他任何工具中进行过测试.
您还可以删除域或在.get(0).href
上分割'#'并使用数组的第二部分,假设它甚至包含'#'(请检查.length
).
http://www.glennjones.net/Post/809/getAttributehrefbug.htm
Can it really be true that the attr("href")
command for a link is handled very different in IE7 in comparison to all other browsers?
Let's say I have a page at http://example.com/page.html and I have this HTML:
<a href="#someAnchor" class="lnkTest">Link text</a>
and this jQuery:
var strHref = $(".lnkTest").attr("href");
Then in IE7 the value of the strHref
variable will be "http://example.com/page.htm#someAnchor"
but in other browsers it will be "#someAnchor"
.
I believe that the last mentioned case is the most correct one, so is it just a case of IE7 being a bad boy or is it a bug in jQuery?
It's certainly not a bug in jQuery but instead browsers' inconsistent implementations of .getAttribute('href')
- I suggest using just .get(0).href
for consistency.
Seems like you can access the attribute text in IE and Mozilla using .get(0).getAttribute('href', 2)
if you don't want the absolute URI. Note however this won't work in Opera and I haven't tested in Safari/Chrome/anything else.
You could also strip out the domain or split on '#' for .get(0).href
and use the second part of the array assuming it even contains '#' ( check .length
).
http://www.glennjones.net/Post/809/getAttributehrefbug.htm
这篇关于与所有其他浏览器相比,在IE7中错误提取.attr("href")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!