如何将JavaScript循环打印到ID元素?

huangapple go评论61阅读模式
英文:

How can I print a JavaScript loop to an element by ID?

问题

The same output I get in a console.log is not posting to the HTML div and I cannot figure out why.
我在console.log中获得的相同输出未发布到HTML div中,我无法弄清原因。

I am expecting this array to print:
我希望这个数组打印出:

["Thief", "The Lion", "Harry Potter", "Lord of the Rings"]
["小偷", "狮子", "哈利·波特", "指环王"]

英文:

The same output I get in a console.log is not posting to the HTML div and I cannot figure out why.

I am expecting this array to print:

["Thief", "The Lion", "Harry Potter", "Lord of the Rings"]

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const myLibrary = [&quot;Thief&quot;, &quot;The Lion&quot;, &quot;Harry Potter&quot;, &quot;Lord of the Rings&quot;];

function Book() {
  // the constructor
}

var bookLength = myLibrary.length;


function addBookToLibrary() {
  for (var b = 0; b &lt; bookLength; b++)
    console.log(myLibrary[b]);
    
  return myLibrary[b];
}

var labels = document.getElementById(&quot;myBooks&quot;).innerHTML;
labels = addBookToLibrary();

<!-- language: lang-html -->

&lt;p&gt;My list should be below&lt;/p&gt;
&lt;p id=&quot;myBooks&quot;&gt;&lt;/p&gt;

&lt;!-- script src=&quot;script.js&quot;&gt;&lt;/script --&gt;

<!-- end snippet -->

答案1

得分: 2

  1. 问题一:您的 labels 变量是innerHTML的值的副本,而不是对它的引用。

您的 labels 变量不是对源元素 innerHTML 的引用。它是在分配时的值的副本。因此,更改 labels 的值不会以任何方式影响源元素。

如果您想要更新元素的 innerHTML,您必须直接进行赋值。(虽然还有其他方法可以做到这一点,但是针对此问题,我关注的是 OP 尝试做的事情。)

  1. 问题二:您循环遍历数组以记录它们,然后返回 undefined

导致 console.log(labels); 返回 undefined 的原因是您的 for 循环会一直递增 b 直到它等于数组的长度,然后您返回 myLibrary[b]

JavaScript 数组是从零开始索引的,这意味着第一个项目是 n[0],最后一个项目是 n[length - 1]

因为在您的 for 循环退出时,b 变量等于 myLibrary.length,所以 myLibrary[b]undefined,这就是您在日志中看到的内容。

英文:

A couple of issues in play:

1. Your labels variable is a copy of innerHTML's value, not a reference to it.

Your labels variable is not a reference to the source element's innerHTML. It is a copy of its value at the time you assigned it. So changing the value of labels does not affect the source element in any way whatsoever.

If you want to update the element's innerHTML you must assign it directly. (There are other ways to do this, but for the purposes of this question I'm focusing on what OP is trying to do.)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const myLibrary = [&quot;Thief&quot;, &quot;The Lion&quot;, &quot;Harry Potter&quot;, &quot;Lord of the Rings&quot;];

document.querySelector(&#39;#myBooks&#39;).innerHTML = myLibrary.join(&#39;&lt;br/&gt;&#39;);

<!-- language: lang-html -->

&lt;p&gt;My list should be below&lt;/p&gt;
&lt;p id=&quot;myBooks&quot;&gt;&lt;/p&gt;

<!-- end snippet -->


2. You loop over the array to log them, then return undefined.

The reason your console.log(labels); call emits undefined is because your for loop advances b until it's the length of the array, and then you return myLibrary[b].

Javascript arrays are zero-indexed, meaning the first item is n[0] and the last item is at n[length - 1].

Because your b variable is equal to myLibrary.length when your for loop exits, myLibrary[b] is undefined, so that's what you see in the log.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const myLibrary = [&quot;Thief&quot;, &quot;The Lion&quot;, &quot;Harry Potter&quot;, &quot;Lord of the Rings&quot;];

function addBook() {
  for(var b = 0; b &lt; myLibrary.length; b++) {
  }
  console.log(b); // 4
  return myLibrary[b]; // myLibrary[4] === undefined;
}

addBook();

<!-- end snippet -->

答案2

得分: -1

以下是您的代码的翻译部分:

const myLibrary = ["Theif", "The Lion", "Harry Potter", "Lord of the Rings"];

function Book() {
  // 构造函数
}

var bookLength = myLibrary.length;
var outer = "";

var labels = document.getElementById("myBooks");
function addBookToLibrary() {
  for (var b = 0; b < bookLength; b++){
    labels.innerHTML += myLibrary[b] + "<br>";
    outer += myLibrary[b] + '\n';
    console.log(myLibrary[b]);
  } 
  return outer;
}

labels = addBookToLibrary();

console.log(labels);
<p>我的列表应该在下面</p>
<p id="myBooks"></p>

<!-- script src="script.js"></script -->

希望这有助于您的工作!如果您需要任何进一步的帮助,请随时提问。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const myLibrary = [&quot;Theif&quot;, &quot;The Lion&quot;, &quot;Harry Potter&quot;, &quot;Lord of the Rings&quot;];

function Book() {
  // the constructor
}

var bookLength = myLibrary.length;
var outer = &quot;&quot;;

var labels = document.getElementById(&quot;myBooks&quot;)
function addBookToLibrary() {
  for (var b = 0; b &lt; bookLength; b++){
    labels.innerHTML += myLibrary[b] + &quot;&lt;br&gt;&quot;
    outer += myLibrary[b] + &#39;\n&#39;
    console.log(myLibrary[b]);
   } 
  return outer;
  
}


labels = addBookToLibrary();

console.log(labels);

<!-- language: lang-html -->

&lt;p&gt;My list should be below&lt;/p&gt;
&lt;p id=&quot;myBooks&quot;&gt;&lt;/p&gt;

&lt;!-- script src=&quot;script.js&quot;&gt;&lt;/script --&gt;

<!-- end snippet -->

答案3

得分: -1

以下是翻译好的代码部分:

const myLibrary = ["Theif", "The Lion", "Harry Potter", "Lord of the Rings"];

function Book(){
    // the constructor
}

function addBookToLibrary(myLibrary) {
    var result = "";
    for (var library in myLibrary){
        console.log(myLibrary[library]);
        result += myLibrary[library] + "<br>";
    }
    return result
}

document.getElementById("myBooks").innerHTML = addBookToLibrary(myLibrary);
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const myLibrary = [&quot;Theif&quot;, &quot;The Lion&quot;, &quot;Harry Potter&quot;, &quot;Lord of the Rings&quot;];

function Book(){
    // the constructor
}

function addBookToLibrary(myLibrary) {
    var result = &quot;&quot;;
    for (var library in myLibrary){
        console.log(myLibrary[library]);
        result += myLibrary[library] + &quot;&lt;br&gt;&quot;;
    }
    return result
}


document.getElementById(&quot;myBooks&quot;).innerHTML = addBookToLibrary(myLibrary);

<!-- end snippet -->

The result I came to. I am confused why the return result worked but remembered i needed it from past lessons, i googled the <br>

huangapple
  • 本文由 发表于 2023年6月1日 05:39:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定