<script>
var MyDiv = function(){} //定义一个类
MyDiv.prototype={ // 定义类的方法
c_d : function(c, w, h){
var d = document.createElement('div');
d.style.color = c || '';
d.w = w || '';
d.h = h || '';
return d;
},

c_t : function(txt){
return document.createTextNode(txt);
}
}
var div = new MyDiv()/*实例化一个类*/, d = div.c_d('blue','100px','20px') /*通过调用类的方法得到一个div元素对象*/, t = div.c_t('传的参数')/*通过调用类的方法得到一个text元素对象*/;

d.appendChild(t);onload = function (){
document.body.appendChild(d);
}</script>

解决方案 »

  1.   

    也就是说..MyDiv 这个类的实例并不是一个DIV了 ?var div = new MyDiv();div 不是一个DIV Element , 而 div.c_d() 会创建一个DIV Element.是这个意思吗>我想得到的是.. div = new MyDiv()以后..div本身就是一个DIV Element 
    然后可以 document.appendChild(div);
    谢谢楼上的回答..继续等等
      

  2.   

    晕,你想这样?
    <script>
        var MyDiv = function(){ return this.c_d()}
        MyDiv.prototype={
            c_d : function(){
                return document.createElement('div');
            }
        }var div = new MyDiv;
    div.style.background="blue";
    div.style.height = "30px";
    div.style.width = "100px";onload = function(){
    document.body.appendChild(div);
    }
    </script>
      

  3.   

    谢谢 老韩.能不能把你这个例子再扩展一下呢比方说.我希望这个类有 new MyDiv().Title的属性. 
      

  4.   

    我都晕,楼主想实现什么这样?
    <script>
        var MyDiv = function(title){ return this.c_d(title) }    MyDiv.prototype={
            c_d : function(title){
                var d = document.createElement('div');
    d.Title = title;
    return d;
            }
        }var div = new MyDiv('div_Title');
    alert(div.Title); // div_Title
        div.style.background="blue";
        div.style.height = "30px";
        div.style.width = "100px";onload = function(){
        document.body.appendChild(div);
    }
    </script>
      

  5.   

    我只是希望var div = new MyDiv();
    div.Title = "xx";
      

  6.   

    老韩..where are you .. 
      

  7.   

    直接定义就行了
    <script>
        var MyDiv = function(){ return this.c_d()}
        MyDiv.prototype={
            c_d : function(){
                return document.createElement('div');
            }
        }var div = new MyDiv;
        div.style.background="blue";
        div.style.height = "30px";
        div.style.width = "100px";
    div.id = 'test'; // 给个ID以便测试 div.Title = 'xx';onload = function(){
        document.body.appendChild(div);
    alert(document.getElementById('test').Title); // 这里提示 xx}
    </script>