2017-01-16 2 views
-1

이미 답변이 있으면 사과드립니다. 아무 것도 발견하지 못했습니다.속성으로 저장된 dom 요소에서 dom을 변경할 수 없습니다.

HTML 코드를 변경해야하는 블록이있는 약간의 JS 코드를 개발 중입니다. 다음 ID 태그로 초기화된다

  • DOM 요소가 변경되어야 getHtml() 메소드를

    • 사용자 정의 객체 : 그래서 나는 속성 2 종류의 주요 목적이있다.

    내 첫 번째 버전이 작동했습니다. 일부 자바 스크립트 이벤트 및 메서드를 설정하면 내 블록 내용이 변경되었습니다.

    물론 새로운 기능을 구현하기 위해 코드를 수정해야했지만 이제는 내 메소드가 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; 
        } 
    } 
    

    빠른 개요

    1. 내 모든 속성을하고이를 초기화하는 데 도움이되는 방법을 설명합니다.
    2. json 데이터를 확인하려면 try catch를 사용하십시오.
    3. 내가 프로그램 여기

    와 상호 작용하는

  • 몇 가지 방법이 제 1 블록이 생성되는 방법 내 특성을 올바르게 구문 분석
    1. JSON 일이 무엇인가 초기화, 반환 첫 번째 콘텐츠 속성에 넣어지는 HTML 코드. 테스트로 dom에 텍스트를 다시 추가합니다. (작동합니다)
    2. 두 번째 블록이 만들어지고 두 번째 내용 속성에 html 코드가 반환됩니다. 첫 번째 컨텐츠 속성에 테스트로 텍스트를 다시 추가합니다. 이 시점에서 실패합니다.
    3. 내 방법이 어떤 방식 으로든 도움이되지 않습니다. 콘텐츠 속성이 응답하지 않는 것 같습니다.

    콘텐츠 속성 값은 여전히 ​​얻을 수 있습니다. atelierScreen.operationMenuContent.innerHTML; 그러나 DOM에 텍스트를 삽입 할 수 없습니다. atelierScreen.operationMenuContent.innerHTML = ""; 아무 작업도 수행하지 않습니다.

    doc.getElement ...로 DOM을 쿼리해야합니까? 각 시간? DOM 요소가 속성으로 사용 된 이전 버전이 작동 중입니다 ...

    누락되었으므로 매우 실망 스럽습니다.) 도와주세요. 감사합니다.

  • 답변

    0

    확인을 줄 것이다,이 나는 것을,

  • 내가 화면 요소를 만드는 시도/캐치 내부 JSON 매개 변수를 구문 분석

    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+"'"; 
    } 
    } 
    
    1. 을 한 것입니다 내 앵커를 초기화하는 데 사용됩니다
    2. 블록 배열을 만들고 같은 시간에 내 개체를 초기화합니다. ork, 만약 그 속성이 나중에 발견된다면, 그렇습니다). Onjects에는 앵커 및 콘텐츠 매개 변수가 포함됩니다. 블록을 추가 할
    3. 1 루프는 HTML 콘텐츠를 만들 수
    4. 2 루프 앵커
  • 0

    일부 연구를 마친 후 innerHTML을 사용했기 때문일 수 있습니다. innerHTML을 사용하여 자식 요소를 파괴하는 것처럼 보입니다.

    나는이 처음에 작업을해야하지만, 내가 좋아하는 뭔가를 시도 할 것이다 :

    • 이 블록 태그와 태그 및 콘텐츠 매개 변수, 생성자
    • 프로토 타입의 getContent (와 "추상적"부모 클래스) 모양 만듭니다 같은 :

      if(content==null) content = document.getElementById(this.tag); 
      return content; 
      
    • 내 개체의 배열 내 주요 "화면"개체를 초기화

    • 태그를 가져 와서 innerHTML을 초기화하는 배열의 루프
    • 객체를 인스턴스화하는 배열의 루프
    • getContent가 나머지를 처리합니다.

    내가 당신에게 피드백

    관련 문제