代码如下
ie6 下 给元素设置属性时,是可以得到元素  可以看出弹出的是obj
在ff下 却很纠结 弹出的是string  我希望的是得到那个元素
那该怎么做了  (pS:表要说保存id,我非常的不喜欢,我宁愿用一个数组去记录也不用id);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>OOXXOO</title> 
</head> 
<div id="ss" style="height:100px; width:100px; background-color:#009933"></div>
<div id="vv" style="height:100px; width:100px; background-color: #990000"></div>
<body>
 
<script language="javascript">
var $ = function(id){return document.getElementById(id);};
window.onload = function(){
$('ss').setAttribute('elm',$('vv'));

alert(typeof $('ss').getAttribute('elm'));
}
</script>
</body> 
</html> 

解决方案 »

  1.   

    这是浏览器设置的问题
    你不加typeof  FF弹出来的就是[object HTMLDivElement]也是objcet类型你做个判断就行了
      

  2.   


    // 看下W3C的接口定义
    DOMString                 getAttribute(in DOMString name);
      void                      setAttribute(in DOMString name, 
                                             in DOMString value)
                                             raises(DOMException);
    getAttribute 返回的是DOMString. 而setAttribute参数定义也是DOMString.
    // 可以理解attribute对应的就是DOM元素HTML形式的属性 比如
    //<div attribute='**这里应该不会是object*'>我认为反而是IE6有问题所以我想你为什么不放到DOM的property里呢?
     $('ss')['elm'] = $('vv');
      

  3.   


    我考虑了下 估计桃子兄 是把attribute和property搞混了 不然一想html标签属性 显然存对象是不可能的 只是DOMString
      

  4.   


    FF下弹出来的也是object,只是FF浏览器的核心设置成string类型了~··
      

  5.   

    我没用过setAttribute,但是直接赋值属性就能显示你想要的结果了
    var $ = function(id){return document.getElementById(id);};
    window.onload = function(){
        //$('ss').setAttribute('elm',$('vv'));
        $('ss').elm=$('vv')
        //alert(typeof $('ss').getAttribute('elm'));
    alert(typeof $('ss').elm);
    }