简体   繁体   English

函数中创建的元素未追加到DOM

[英]Created Element in function not appending to DOM

I am having some issues figuring out why my created Element won't append to the DOM within my function. 我在弄清楚为什么我创建的Element无法在函数中附加到DOM时遇到了一些问题。

 let startText = document.querySelector('.text-container').innerHTML = "Lets Start."; let lvlTitle = document.querySelector('.text-container'); let background = document.querySelector('.full-page'); lvlTitle.addEventListener('click', countChangeBackground); var clicks = 0; function countChangeBackground() { clicks += 1; var message = ""; if (clicks == 1) { message = "Test"; background.style.backgroundColor = "#f81884"; lvlTitle.style.color = "#f2eee2"; } else if (clicks == 2) { message = "Test2"; background.style.backgroundColor = "#f5ce28"; lvlTitle.style.color = "black"; } else if (clicks == 3) { message = "Add Li to ul"; var ul = document.querySelector('.text-container'); var li = document.createElement('li'); li.className = 'text-content'; li.appendChild(document.createTextNode('New Text')); ul.appendChild(li); console.log(li); } else { message = startText; background.style.backgroundColor = "#f2eee2"; lvlTitle.style.color = "black"; } lvlTitle.innerHTML = message; }; 
 <div class="full-page"> <div class="click-container"> <ul class="text-container"> </ul> </div> </div> 

And here is a jsfiddle: Append Li to DOM 这是一个jsfiddle: 将Li附加到DOM

This is the line causing you problems: 这是导致您出现问题的行:

lvlTitle.innerHTML = message;

When you set the innerHTML, if there are other nodes inside of that element, they are overwritten. 设置innerHTML时,如果该元素内还有其他节点,则它们将被覆盖。

You need to alter your logic to only set the innerHTML in some cases. 在某些情况下,您需要更改逻辑以仅设置innerHTML。

I've updated your jsfiddle here: http://jsfiddle.net/jepoqd01/ 我在这里更新了您的jsfiddle: http : //jsfiddle.net/jepoqd01/

You are overwriting the entire container with innerHTML and not reaching the correct content text 您正在用innerHTML覆盖整个容器,但未到达正确的内容文本

See solution add new variable lvlContainer with get the .text-container and lvlText get the .text-content 查看解决方案,添加新变量lvlContainer,获取.text-container,lvlText获取.text-content

let startText = document.querySelector('.text-content').innerHTML = "Lets Start.";
let lvlTitle = document.querySelector('.text-content');
let lvlContainer = document.querySelector('.text-container');
let background = document.querySelector('.full-page');

lvlTitle.addEventListener('click', countChangeBackground);

var clicks = 0;

function countChangeBackground() {
  clicks += 1;
  var message = "";
  if (clicks == 1) {
    message = "Test";
    background.style.backgroundColor = "#f81884";
    lvlContainer.style.color = "#f2eee2";
  } else if (clicks == 2) {
    message = "Test2";
    background.style.backgroundColor = "#f5ce28";
    lvlContainer.style.color = "black";

  } else if (clicks == 3) {
    message = "Add Li to ul";

    var ul = document.querySelector('.text-container');
    var li = document.createElement('li');

    li.className = 'text-content';
    li.appendChild(document.createTextNode('New Text'));
    ul.appendChild(li);
    console.log(li);
  } else {
    message = startText;
    background.style.backgroundColor = "#f2eee2";
    lvlContainer.style.color = "black";
  }
  lvlTitle.innerHTML = message;
};

is in jsfiddle: http://jsfiddle.net/lucaslimax/7jx194gp/37 在jsfiddle中: http : //jsfiddle.net/lucaslimax/7jx194gp/37

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM