EmK*_*Kay 30 jquery relative-path href absolute-path internet-explorer-7
难道真的是不错,attr("href")提供的链接命令在IE7相比,处理得非常不同于其他的浏览器?
假设我在http://example.com/page.html上有一个页面,我有这个HTML:
<a href="#someAnchor" class="lnkTest">Link text</a>
Run Code Online (Sandbox Code Playgroud)
这个jQuery:
var strHref = $(".lnkTest").attr("href");
Run Code Online (Sandbox Code Playgroud)
然后在IE7中strHref变量的值将是,"http://example.com/page.htm#someAnchor"但在其他浏览器中它将是"#someAnchor".
我相信最后提到的案例是最正确的案例,所以它只是IE7是一个坏男孩的案例,还是jQuery中的一个错误?
med*_*iev 18
它肯定不是 jQuery中的错误,而是浏览器的不一致实现.getAttribute('href')- 我建议只是.get(0).href为了保持一致性.
.get(0).getAttribute('href', 2)如果您不想使用绝对URI,似乎可以使用IE和Mozilla中的属性文本.但是请注意,这在Opera中不起作用,我还没有在Safari/Chrome /其他任何地方进行过测试.
您也可以删除域或拆分'#' .get(0).href并使用数组的第二部分,假设它甚至包含'#'(检查.length).
http://www.glennjones.net/Post/809/getAttributehrefbug.htm
| 归档时间: |
|
| 查看次数: |
10279 次 |
| 最近记录: |