日期:2014-05-16  浏览次数:20322 次

定义一个继承自div的对象
function Node()
{
}

如果使得Node继承自div对象

------解决方案--------------------
 

 function Node(html){
  var el=document.createElement('Div');
  for(var k in  this) el[k]=this[k];
  el.innerHTML=html;
  return el;
 }
 Node.prototype.hide=function(){   //添加隐藏方法
  this.style.display="none"
 }
 var node=new Node( 'ok'  ) //实例 并且 HTML = ok
 document.body.appendChild( node   );
 setTimeout( function(){ 
   node.hide();  //隐藏
 },1500 )


------解决方案--------------------
function mydiv(){
var div = document.createElement('div'); 
for(var p in this.constructor.prototype){
        div[p] = this.constructor.prototype[p]
}
return div;
}
mydiv.prototype.kk = function(){alert('kk')}

var md = new mydiv();

md.kk();
//md is HTMLDIVelement
//md has kk function()