Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从页面中删除对象_Javascript_Oop - Fatal编程技术网

Javascript 从页面中删除对象

Javascript 从页面中删除对象,javascript,oop,Javascript,Oop,如何从页面中删除对象?这是我的OOP代码,带有多态性 function Container() { this.id = ''; this.className = ''; this.htmlCode = ''; } Container.prototype.remove = function () { window.onload = function () { var elem = document.getElementById(this.id);

如何从页面中删除对象?这是我的OOP代码,带有多态性

function Container() {
    this.id = '';
    this.className = '';
    this.htmlCode = '';
}

Container.prototype.remove = function () {
    window.onload = function () {
        var elem = document.getElementById(this.id);
        elem.parentNode.removeChild(elem);
        return elem.id;
    }
}

Container.prototype.render = function () {
    return this.htmlCode;
};

function Menu(myId, myClass, myItems) {
    Container.call(this);
    this.id = myId;
    this.className = myClass;
    this.items = myItems;
}

Menu.prototype = Object.create(Container.prototype);
Menu.prototype.constructor = Menu;

Menu.prototype.render = function () {
    var res = '<ul class="'+this.className+'">';

    for (var item in this.items)
    {
        if(this.items[item] instanceof MenuItem){
            res += this.items[item].render();
        }
    }
    res += '</ul>';
    return res;
};

function MenuItem(myHref, myName) {
    Container.call(this);
    this.className = 'menu-item';
    this.href = myHref;
    this.name = myName;
}

MenuItem.prototype = Object.create(Container.prototype);
MenuItem.prototype.constructor = MenuItem;

MenuItem.prototype.render = function () {
    return '<li><a href="'+this.href+'">'+this.name+'</a></li>';
};


var menu = new Menu('my_menu', 'my_menu', [
    new MenuItem('/', 'Main'),
    new MenuItem('/about/', 'About us'),
    new MenuItem('/contacts', 'Contacts'),
    new MenuItem('/data', "Data"),
])

我每次都收到未捕获的类型错误:无法读取属性不要使用
文档。write()
。此外,我还解决了一些问题:

函数容器(){
this.id=“”;
this.className='';
this.htmlCode='';
}
Container.prototype.remove=函数(){
var elem=document.getElementById(this.id);//只需删除元素,不要等待“window.onload”
elem.parentNode.removeChild(elem);
}
Container.prototype.render=函数(){
返回此.htmlCode;
};
功能菜单(myId、myClass、myItems){
容器。调用(此);
this.id=myId;
this.className=myClass;
this.items=myItems;
}
Menu.prototype=Object.create(Container.prototype);
Menu.prototype.constructor=菜单;
Menu.prototype.render=函数(){
//将id添加到
    元素! var res='
      ; for(此.items中的var项) { if(此.items[item]实例为菜单项){ res+=this.items[item].render(); } } res+='
    '; 返回res; }; 函数菜单项(myHref,myName){ 容器。调用(此); this.className='菜单项'; this.href=myHref; this.name=myName; } MenuItem.prototype=Object.create(Container.prototype); MenuItem.prototype.constructor=MenuItem; MenuItem.prototype.render=函数(){ 返回“
  • ”; }; var menu=新菜单(“我的菜单”、“我的菜单”[ 新菜单项(“/”,“Main”), 新菜单项(“/about/”,“about us”), 新菜单项(“/contacts”、“contacts”), 新菜单项(“/data”,“data”), ]); var nav=document.querySelector('nav'); document.getElementById('render')。addEventListener('click',function(){ nav.innerHTML=menu.render(); }); document.getElementById('remove').addEventListener('click',函数(){ menu.remove(); });
    
    伦德尔
    删除
    document.write(menu.render());
    document.write(menu.remove());
    console.log('ok');