Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/151.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 我能';t从作为属性存储的dom元素更改dom_Javascript_Dom_Innerhtml - Fatal编程技术网

Javascript 我能';t从作为属性存储的dom元素更改dom

Javascript 我能';t从作为属性存储的dom元素更改dom,javascript,dom,innerhtml,Javascript,Dom,Innerhtml,我很抱歉,如果已经有答案,我发现没有什么有用的 我正在开发一个小JS代码,其中包含一些需要更改html代码的块。 因此,我有一个具有两种属性的主对象: 使用getHtml()方法自定义对象 然后应该更改用id标记初始化的dom元素 我的第一个版本成功了:我设置了一些javascript事件和方法,然后我的块内容被更改了 当然(!)我需要修改代码以实现新功能,现在我的方法返回html代码,但是变量上的.innerHTML不再起作用 让我给你看一些代码并解释一下 var AtelierScree

我很抱歉,如果已经有答案,我发现没有什么有用的

我正在开发一个小JS代码,其中包含一些需要更改html代码的块。 因此,我有一个具有两种属性的主对象:

  • 使用getHtml()方法自定义对象
  • 然后应该更改用id标记初始化的dom元素
我的第一个版本成功了:我设置了一些javascript事件和方法,然后我的块内容被更改了

当然(!)我需要修改代码以实现新功能,现在我的方法返回html代码,但是变量上的.innerHTML不再起作用

让我给你看一些代码并解释一下

var AtelierScreen = function(ancre, dataJson){
    if(!ancre.charAt)               throw "Invalid string-dealer name type ('"+ancre+"') should be String";
    if(ancre.length==0)             throw "Invalid string-dealer name ('"+ancre+"') cannot be empty";

    this.atelierContent;
    this.operationMenu;
    this.operationMenuContent;
    this.operationRecap;
    this.operationRecapContent;

    this.create=function(obj,tagsuffix){
        var tag = ancre+tagsuffix;
        this.atelierContent.innerHTML += "<div id='"+tag+"'></div>";
        var content = document.getElementById(tag);
        if(!content)        throw "Invalid html anchor using '"+tag+"' as an html id";
        content.innerHTML=obj.getHtml();
        return content;
    }

    try{
        var dataObject = JSON.parse(dataJson);
        this.atelierContent = document.getElementById(ancre);
        if(!this.atelierContent)            throw "Invalid html anchor using '"+ancre+"' as an html id";

        // MENU
        this.operationMenu = new OperationMenu(dataObject["dealer"],dataObject["operations"]);
        this.operationMenuContent=this.create(this.operationMenu,"_menu");
        this.operationMenuContent.innerHTML+="after init";

        // RECAP
        this.operationRecap = new OperationRecap();
        this.operationRecapContent=this.create(this.operationRecap,"_recap");
        this.operationMenuContent.innerHTML+="after recap init";

    } catch(error){
        throw "Error decoding json data :\n'"+error+"'\n\nJson =\n'"+dataJson+"'";
    }

    this.setSelectedModel=function(model){
        this.operationMenuContent.innerHTML+="setSelectedModel";
        var isTheSame = this.operationMenu.setSelectedModel(model);
        if(!isTheSame) this.clearOperationItemFromRecap();
        var temp = this.operationMenu.getHtml()
        this.operationMenuContent.innerHTML=temp;
    }
}
var AtelierScreen=function(ancre,dataJson){
如果(!ancre.charAt)抛出“无效字符串,则经销商名称类型(“+ancre+”)应为字符串”;
如果(ancre.length==0)抛出“无效字符串交易商名称(“+ancre+”)不能为空”;
这是内容;
这是操作菜单;
本操作菜单内容;
这是操作概述;
这是操作的内容;
this.create=函数(obj,tagsuffix){
var标记=ancre+标记后缀;
this.atelierContent.innerHTML+=“”;
var content=document.getElementById(标记);
如果(!content)抛出“使用“+”标记“+”作为html id的无效html锚”;
content.innerHTML=obj.getHtml();
返回内容;
}
试一试{
var dataObject=JSON.parse(dataJson);
this.atelierContent=document.getElementById(ancre);
如果(!this.atelierContent)抛出“使用''+ancre+''作为html id的无效html锚”;
//菜单
this.operationMenu=新操作菜单(数据对象[“经销商”]、数据对象[“操作”]);
this.operationMenuContent=this.create(this.operationMenu,“\u menu”);
this.operationMenuContent.innerHTML+=“在初始化之后”;
//重述
this.operationRecap=新的operationRecap();
this.operationRecapContent=this.create(this.operationRecap,“_-recap”);
this.operationMenuContent.innerHTML+=“在重述初始化之后”;
}捕获(错误){
抛出“解码json数据时出错:\n''+Error+'\n\nJson=\n'+dataJson+'”;
}
this.setSelectedModel=函数(模型){
this.operationMenuContent.innerHTML+=“setSelectedModel”;
var isTheSame=this.operationMenu.setSelectedModel(模型);
如果(!IsTheName)此.clearOperationItemFromRecap();
var temp=this.operationMenu.getHtml()
this.operationMenuContent.innerHTML=temp;
}
}
快速概述

  • 我的所有属性和帮助初始化它们的方法
  • 检查json数据的尝试捕获
  • 我使用该方法初始化属性
  • 与程序交互的一些方法
  • 发生了什么

  • 正确解析中的Json
  • 创建我的第一个块后,返回放入第一个内容属性的html代码。我再次向dom中添加一些文本作为测试(它可以工作)
  • 创建第二个块后,返回放入第二个内容属性的html代码。我在第一个内容属性中再次添加一些文本作为测试。它在这一点上失败了
  • 我的方法没有任何帮助。我的内容属性似乎没有响应
  • 我仍然可以获得内容属性值 AtelerScreen.operationMenuContent.innerHTML; 但是我不能在dom中插入文本。 AtelerScreen.operationMenuContent.innerHTML=“”; 什么也不做

    是否需要使用document.getElement查询dom。。。每次? 我以前使用dom元素作为属性的版本正在运行

    我错过了一些东西,这太令人沮丧了;) 请帮忙。
    谢谢

    经过一些研究,可能是因为使用了innerHTML。 看起来像是使用innerHTML销毁子元素

    我需要先解决这个问题,但我会尝试以下方法:

    • 使用标记和内容参数创建块“抽象”父类,使用标记创建构造函数
    • 原型getContent()将类似于:

      if(content==null) content = document.getElementById(this.tag);
      return content;
      
    • 用我的对象数组初始化我的主“屏幕”对象

    • 循环数组以获取标记并初始化innerHTML
    • 在数组上循环以实例化对象
    • getContent将处理其余部分
    我会给你一些反馈好的,这就是我所做的

    var AtelierScreen = function(ancre, dataJson){
    try{
        var dataObject = JSON.parse(dataJson);
        this.atelierContent = document.getElementById(ancre);
        if(!this.atelierContent)            throw "Invalid html anchor using '"+ancre+"' as an html id";
    
        var blocks = [
            this.operationMenu   = new OperationMenu(...),
            this.operationRecap  = new OperationRecap(...),
            this.operationResult = new OperationResult(...)
        ];
    
        for (var i = 0; i < blocks.length; i++) {
            var block=blocks[i];
            this.atelierContent.innerHTML += "<div id='"+block.tag+"'></id>";
        }
    
        for (var i = 0; i < blocks.length; i++) {
            var block=blocks[i];
            block.getContent().innerHTML = block.getHtml();
        }
    } catch(error){
        throw "Error decoding json data :\n'"+error+"'\n\nJson =\n'"+dataJson+"'";
    }
    }
    
    var AtelierScreen=function(ancre,dataJson){
    试一试{
    var dataObject=JSON.parse(dataJson);
    this.atelierContent=document.getElementById(ancre);
    如果(!this.atelierContent)抛出“使用''+ancre+''作为html id的无效html锚”;
    变量块=[
    this.operationMenu=新建operationMenu(…),
    this.operationRecap=新的operationRecap(…),
    this.operationResult=新的operationResult(…)
    ];
    对于(变量i=0;i
  • 我在try/catch中解析Json参数
  • 我创建了screen元素,它将用于初始化我的锚
  • 我创建了我的块数组,同时初始化了我的对象(如果以后能找到这些属性,我不确定它是否有效,但它确实有效)。onject包含它们的锚和内容参数
  • 第一个循环用于添加块和锚
  • 创建html内容的第二个循环