我有一个已知的document对象<div aa='1' bb='2' ...></div>Div里面的对象是未知的,这样,怎么取得只有Div中才存在的属性?aa,bb等,如果id是div的属性,但没有赋值的话
也不要取出来,这样的东西要怎么实现?????????

解决方案 »

  1.   

    jquery方法:$('div').attr('aa');$('div').attr('bb')只会取  div 下的 aa的内容和bb的内容
      

  2.   


     for(var attr in domobj)
    {
      //if(条件)
       alert(i + ":" + domobj[i]);
    }????
      

  3.   


    这样做,什么都会啊,问题是,aa与bb是未知的啊!
      

  4.   

    <html><head>
    <script type="text/javascript">  
    function showProp(obj) {
    var test = obj; var tagProp = test.outerHTML; var tagName = test.tagName;

    var props = tagProp.substring(tagProp.indexOf(tagName) + tagName.length, tagProp.indexOf(">"));

    var retArr = {}; var propList = props.split(' '); for (var index = 0; index < propList.length; index++) {
    if (propList[index] == null || propList[index] == "") {
    continue;
    }
    var currentProp = propList[index].split('='); while(currentProp[1].indexOf("\"") != -1) {
    currentProp[1] = currentProp[1].replace("\"", "");
    }

    retArr[currentProp[0]] = currentProp[1];
    }
    return retArr;
    }function test() {
    var test = document.getElementById('test');
    var propList = showProp(test);
    for (var prop in propList) {
    alert(prop);
    alert(propList[prop]);
    }
    }
    </script>
    </head><body>
    <input type="button" onclick="test();"></input>
    <div id="test" aa="b" c=11 name="aa"></div>
    </body></html>
      

  5.   


    你的就和domobj.attributes差不多啊一百多个对象,其实,这中间我一个属性都没有设置!
      

  6.   

    也可以使用4楼的思路,去文档中查找。
    但像style这样的属性分析起来稍有点麻烦style = " background-image:..."
      

  7.   


    <script>
    window.onload=function(){
      var div = document.getElementsByTagName("div")[0];
      var s = div.outerHTML;
      s = s.replace(/>.*/g,">");
      var re = /(\w+)=('|")([^\2]+?)\2/ig;
      var m
      while((m = re.exec(s))!=null){
          alert(m[1] + "的值" + m[3])
      }
    }
    </script>
    <div aa='1 2' bb='2 34'><div>xxx</div></div>
      

  8.   

     这个不行哦~~~我提供一个这个,好像不能排除一些自带产生的属性值,比如tabIndex和disabled之类默认的~~ 献上代码: $(document).ready(function() {
                $("#Btn1").click(function() {
                    var attrs = $("div")[0].attributes; //过去div所包含的所有属性,自带的和自定义的
                    var k = 0;
                    var Str = "";
                    for (var i = 0; i < attrs.length; i++) {
                        if (attrs[i].value.toString() != "null" && attrs[i].value.toString() != "") {//去掉有该属性但无值的情形,但是还是未排除默认的属性
                            k++;
                            Str += "第" + k + "个属性" + attrs[i].name + "值:" + attrs[i].value + "\n\r";
                        }
                    }
                    alert(Str);            });        });html:<input type="button" id="Btn1" value="测试DIV属性" />
        <div id="nn" aa="dd" bb="cc" ii="dd" p="dd" class="dd" align="center" atomicselection="false"
            accesskey="d" lang="aa" hidefocus="hidefocus">
        </div>
      

  9.   


    <script>
    window.onload=function(){
     var div = document.getElementsByTagName("div")[0];
      var s = div.outerHTML;
      s = s.replace(/>.*/g,">");
      var re = /(\w+)="([^"]+?)"/ig;
      var a = s.match(re);
      if(a!=null){
      for(var i=0;i<a.length;i++){
       var t = a[i].split("=");
        alert(t[0] + "的值" + t[1].substr(1,t[1].length-2));
        s = s.replace(a[i],"");
      }
      }
      re = /(\w+)=([\S^>]+)(?=\s|>)/ig;
      a = s.match(re);
      if(a!=null){
      for(var i=0;i<a.length;i++){
       var t = a[i].split("=");
        alert(t[0] + "的值" + t[1]);
        s = s.replace(a[i],"");
      }
      }
      re = /(\s)(\w+?)(?=\s|>)/ig;
      a = s.match(re);
      if(a!=null){
      for(var i=0;i<a.length;i++){
       t = a[i].substr(1, s.length)
        alert(t + "的值" + t);
      }
      }
    }
    </script><div id="nn" aa="dd" bb="cc" ii="dd" p="dd" class="dd" align="center" atomicselection="false" accesskey="d" lang="aa" hidefocus="hidefocus" >
    </div>