简体   繁体   English

我认为代码是相同的,但我不知道为什么它会产生不同的结果

[英]i think that the code is the same but i don't know why it make different results

Why does the code make difference output and it is almost the same input?为什么代码有所不同 output 并且几乎是相同的输入? Input:输入:

var Add = document.getElementById('AddElement'); //the button
var Element = document.getElementById('element'); // the input text
var ListParent = document.querySelector('ul');
    
Add.addEventListener('click', function(){
    var AddText = document.createElement('li').appendChild(document.createTextNode("hello"));
    ListParent.appendChild(AddText);
})

Output: Output:

first click: hello第一次点击:你好

second click: hellohello第二次点击:你好你好

Input:
var Add = document.getElementById('AddElement');
var Element = document.getElementById('element');
var ListParent = document.querySelector('ul');
    
Add.addEventListener('click', function(){
    var AddText = document.createElement('li');
    AddText.appendChild(document.createTextNode('hello'));   
    ListParent.appendChild(AddText);
})

Output: first click: Output:首先点击:

  • hello你好

second click:第二次点击:

  • hello你好
  • hello你好

appendChild returns the appended node: appendChild返回附加的节点:

 const div = document.createElement('div'); const parent = document.createElement('div'); const result = parent.appendChild(div); console.log(result === div);

So when you do所以当你这样做时

var AddText=document.createElement('li').appendChild(document.createTextNode("hello"));
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The value put into addText is the created text node.放入addText的值是创建的文本节点。 In contrast, with与...相比

var  AddText = document.createElement('li');

it's the <li> , not the text node.它是<li> ,而不是文本节点。

So, with the first approach, the created <li> doesn't get used (and probably isn't what you want).因此,使用第一种方法,创建的<li>不会被使用(并且可能不是您想要的)。

If you wanted to use the <li> , and slim your code down, assign to the .textContent of the <li> instead.如果您想使用<li>并精简代码,请改为分配给<li>.textContent

Add.addEventListener('click', function(){
    ListParent.appendChild(document.createElement('li'))
      .textContent = element.value;
})

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

相关问题 为什么这两个函数有不同的结果?(对不起,我不知道如何描述它们) - Why do these two functions have different results?(Sorry, I don't know how to describe them) 我不知道如何将用户输入添加回 DOM,我认为这与我的 js 代码有关 - i don't know how to add the user input back to the DOM and i think its something with my js code 当我认为不是时,代码表现得好像它是异步的 - Code acting like it is asynchronous when I don't think it is 不知道为什么CanvasJS图表代码导致空白页 - Don't know why CanvasJS charting code results in blank page 我不知道为什么这个画布为空 - I don't know why this canvas is null 我不知道为什么 HttpMessageNotReadableException - I don't know why HttpMessageNotReadableException 这个旋钮没有更新,我也不知道为什么 - This knob is not updating and i don't know why 为什么我在附加的代码中没有得到 3 次相同的输出? - Why don't I get the same output 3 times in the attached code? 我陷入了multiplicativePersistence算法问题。 我不知道为什么这段代码不起作用 - I am stuck on multiplicativePersistence algorithm question. I don't know why this code is not working 为什么这个 javscript 代码给出未定义的结果? (计算GCD),不知道为什么不进入if block - why this javscript code giving undefined as a result? (Calculating GCD), I don't know why it's not entering if block
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM