在IE7与所有其他浏览器错误提取.attr(“href”)?
在所有其他浏览器中,链接的attr("href")命令在IE7中的处理方式是否完全不同? 
比方说,我在http://example.com/page.html有一个页面,我有这个HTML:
 <a href="#someAnchor" class="lnkTest">Link text</a> 
和这个jQuery:
 var strHref = $(".lnkTest").attr("href"); 
 然后在IE7中, strHrefvariables的值是"http://example.com/page.htm#someAnchor"但在其他浏览器中则是"#someAnchor" 。 
我认为最后提到的情况是最正确的,那么IE7只是一个坏男孩,还是jQuery中的一个bug呢?
 这当然不是 jQuery中的错误,而是浏览器的.getAttribute('href')不一致的实现 – 我build议只使用.get(0).href来保持一致性。 
 好像你可以使用.get(0).getAttribute('href', 2)在IE和Mozilla中访问属性文本,如果你不想要绝对的URI。 但是,请注意,这不会在Opera中工作,我还没有在Safari / Chrome /其他任何testing。 
 你也可以.get(0).href域名或者在.get(0).href '#'上分割,假设它甚至包含'#'(check .length ),那么使用数组的第二部分。 
我相信它在所有IE 7+中都是这样实现的。
我用:
 var href=jQuery('#foo').attr('href'); href=href.substring(href.indexOf('#')); 
希望它有帮助! 干杯。
我发现了一个与这个问题有关的bug: http : //bugs.jquery.com/ticket/2747 jQuery实现了IE7“bug”的解决方法。 但是在jQuery 1.7.1中,这个bug被重新引入。 我为1.7.1创build了一个新的bug: http : //bugs.jquery.com/ticket/11129
我用:
 var hrefArr = $(this).attr('href').split('/'); var id = hrefArr[hrefArr.length-1]; 
当我在最后一个“/”之后需要一切时。
另一种方法是只使用数据属性,而不是href
 <a data-href="#anchor-0">example</a> 
。
 var href = $(this).attr('data-href'); 
我最终通过PHP创build了一个variables,然后使用javascript replace()方法将它从href中删除:
 <script>var domain = 'http://<?=$_SERVER['HTTP_HOST']?>';</script> <script> $(function(){ /* prevent default action of all anchors with hash class */ $('#canvas').on('click', 'a.hash', function(event) { event.preventDefault(); // get the href of the anchor var hash = '!' + $(this).attr('href'); // remove the absolute url if it exists hash = hash.replace( domain, '' ); // redirect window.location.hash = hash; }); }); </script> 
问题是,IE7和IE8也改变了文字。 所以一个很好的解决办法就是这样做
 $('#linkId').attr('href','newlink').text('oldtext');