当前位置: 首页 > 图文教程 > 网页制作 > CSS样式表 > IE下href 的 BUG问题

CSS样式表
提高网页效率的14条注意事项图文
提高网页的效率 Use YSlow to know why your web Slow
学习WEB标准总结的一些CSS/XHTML知识小结
CSS文件可维护、可读性提高指南
Firefox下样式设置宽度奇怪现象
Chrome的hack写法以及CSS的支持程度图示
IE6支持position:fixed完美解决方法
css为图片设置背景图片
ASP、PHP与javascript根据时段自动切换CSS皮肤的代码
css图片切换效果代码[不用js]
css import与link的区别
BS项目中的CSS架构_仅加载自己需要的CSS
div结合css布局bbs首页(div+css布局入门)
css 兼容性问题this.style.cursor=''''hand''''
CSS渐变统计柱形图
跨浏览器的实践:position:fixed 层的固定定位
标准布局常见问题及解决办法
css美化input file按钮的代码方法
重置默认样式 css reset
支持IE6 IE7 Firefox 的纯CSS的下拉菜单

CSS样式表 中的 IE下href 的 BUG问题


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 214 ::
收藏到网摘: n/a

在浏览器 IE6 、IE7、Firefox2+、Firefpx3+、Opera9.6+、Safari3.1+中测试以下代码
复制代码 代码如下:

<div id="test">
<a href="#"> test </a>
</div>
<div id="result"></div>
<script type="text/javascript">
(function(){
var test = document.getElementById('test');
alert(test.innerHTML);
var result = document.getElementById('result');
result.innerHTML = test.innerHTML;
alert(result.innerHTML)
})();
</script>

结果会发现,在 IE6、IE7 浏览器中第二次弹出的 result.innerHTML 中的 A 元素的 href 值成为了绝对路径。
其实先人们早遇到这些问题(感谢 玉伯 提供的资料):
  • 《getAttribute(”HREF”) is always absolute》
  • 《getAttribute href bug》
    在上面的文章中已提及了处理方案,就是在 IE 下使用 getAttribute( ‘href' , 2 ) 方法。 Microsoft 给此方法扩展了第二个参数,可设置为 0、1、2,如果设置为 2 ,则返回属性原始值。
    脚本修正为:
    复制代码 代码如下:

    (function(){
    var test = document.getElementById('test');
    alert(test.innerHTML);
    var result = document.getElementById('result');
    result.innerHTML = test.innerHTML;
    if(/*@cc_on!@*/0 ) { //if ie
    var links1 = test.getElementsByTagName('a');
    var links2 = result.getElementsByTagName('a');
    for(var i = 0, len = links1.length; i < len; ++i ) {
    links2[i].href = links1[i].getAttribute('href', 2);
    }
    }
    alert(result.innerHTML);
    })();

    在寻找此问题的过程中还搜索到 Hedger Wang 发现的一个有趣的 BUG 问题:在 IE 中当重新设置新的 href 属性值时,如果链接文字含有 “http://” 或 “@” ,则其 innerHTML 将显示不正确,显示成设置的 href 属性。
    解决方法(sHref 为要设置的 href 新值):
    复制代码 代码如下:

    sHref = 'http://www.hedgerwow.com';
    var isMSIE = /*@cc_on!@*/false;
    if( isMSIE ){
    sHref = ' ' + sHref; //add extra space before the new href
    };

    详细:《Internet Explorer might reset Anchor's innerHTML incorrectly when a new “href” is assigned》