英文:
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 = ["Thief", "The Lion", "Harry Potter", "Lord of the Rings"];
function Book() {
// the constructor
}
var bookLength = myLibrary.length;
function addBookToLibrary() {
for (var b = 0; b < bookLength; b++)
console.log(myLibrary[b]);
return myLibrary[b];
}
var labels = document.getElementById("myBooks").innerHTML;
labels = addBookToLibrary();
<!-- language: lang-html -->
<p>My list should be below</p>
<p id="myBooks"></p>
<!-- script src="script.js"></script -->
<!-- end snippet -->
答案1
得分: 2
- 问题一:您的
labels
变量是innerHTML的值的副本,而不是对它的引用。
您的 labels
变量不是对源元素 innerHTML
的引用。它是在分配时的值的副本。因此,更改 labels
的值不会以任何方式影响源元素。
如果您想要更新元素的 innerHTML
,您必须直接进行赋值。(虽然还有其他方法可以做到这一点,但是针对此问题,我关注的是 OP 尝试做的事情。)
- 问题二:您循环遍历数组以记录它们,然后返回
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 = ["Thief", "The Lion", "Harry Potter", "Lord of the Rings"];
document.querySelector('#myBooks').innerHTML = myLibrary.join('<br/>');
<!-- language: lang-html -->
<p>My list should be below</p>
<p id="myBooks"></p>
<!-- 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 = ["Thief", "The Lion", "Harry Potter", "Lord of the Rings"];
function addBook() {
for(var b = 0; b < 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 = ["Theif", "The Lion", "Harry Potter", "Lord of the Rings"];
function Book() {
// the constructor
}
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);
<!-- language: lang-html -->
<p>My list should be below</p>
<p id="myBooks"></p>
<!-- script src="script.js"></script -->
<!-- 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 = ["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);
<!-- 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论