英文:
why does my function work in the console but I can't return it?
问题
我有一个包含20个对象的数组,我的网页上只显示其中的10个,但我想在点击按钮时显示接下来的10个。我创建了一个函数,当点击按钮时它可以工作并被调用,但它只在控制台中工作,我无法返回它。
这是调用该函数的按钮:
<button id="next" onclick="nextTwo()">2</button>
这是函数本身(objList
是包含20个对象的数组):
function nextTwo() {
const page = 2;
const step = 10;
const start = page * step - step;
const end = start + step;
const nextobj = objList.slice(start, end);
console.log(nextobj);
return nextobj;
}
我尝试添加一个 for 循环并添加以下代码:
document.getElementById('x_list').innerHTML = nextobj;
但似乎没有什么效果。
英文:
I have an array with 20 objects, only 10 of them are displayed in my website page, but I'd like to display the next 10 when clicking a button. I created a function that works and gets called when clicking said button but it only works in the console and I can't return it.
This is the button that calls the function:
<button id="next" onclick="nextTwo()">2</button>
and this is the function (the objList is the array containing 20 objects):
function nextTwo() {
const page = 2;
const step = 10;
const start = page * step - step;
const end = start + step;
const nextobj = objList.slice(start, end);
console.log(nextobj);
return nextobj;
}
I tried adding a for loop and adding
document.getElementbyId('x_list').innerHTML = nextobj
but nothing seems to be working.
答案1
得分: 1
innerHTML需要分配一个字符串。 您需要将secondpage数组转换为格式化的字符串,以便在HTML中显示。
// 假设您有一个带有id为'x_list'的容器元素来显示对象
const container = document.getElementById('x_list');
// 在添加新内容之前清除容器
container.innerHTML = '';
// 将数组转换为HTML字符串并附加到容器
nextobj.forEach(obj => {
const item = document.createElement('div');
item.textContent = obj.title; // 假设每个对象都有一个'title'属性来显示
container.appendChild(item);
});
英文:
innerHTML expects a string to be assigned. You need to convert the secondpage array into a formatted string that can be displayed in the HTML.
// Assuming you have a container element with id 'x_list' to display the objects
const container = document.getElementById('x_list');
// Clear the container before adding the new content
container.innerHTML = '';
// Convert the array to an HTML string and append it to the container
nextobj.forEach(obj => {
const item = document.createElement('div');
item.textContent = obj.title; // Assuming each object has a 'title' property to display
container.appendChild(item);
});
答案2
得分: 0
我已经尝试过你的代码,它运行正常。这里是一个 演示,你的代码中的返回语句似乎也正常工作。还请检查一下你的函数闭合大括号是否设置正确,并查看控制台是否有 "illegal return statement" 错误。但是代码正常运行。
const objList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
function nextTwo() {
const page = 2;
const step = 10;
const start = page * step - step;
const end = start + step;
const nextobj = objList.slice(start, end);
//console.log(nextobj);
return nextobj;
}
const nextobj = nextTwo()
console.log(nextobj)
英文:
I have tried your code and it works fine Here is a fiddle where the return statement in your code seems to be working fine. Also check that your function closing braces are set properly and check your console for "illegal return statement" error. But code works fine.
const objList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 ,20]
function nextTwo() {
const page = 2;
const step = 10;
const start = page * step - step;
const end = start + step;
const nextobj = objList.slice(start, end);
//console.log(nextobj);
return nextobj;
}
const nextobj = nextTwo()
console.log(nextobj)
答案3
得分: 0
对于映射电影标题,您需要将标题映射到HTML元素的包装器上。因此,您可以参考下面的代码片段。
const movieList = [
{
"title": "After Dark in Central Park"
},
{
"title": "Boarding School Girls' Pajama Parade"
},
{
"title": "Buffalo Bill's Wild West Parad"
},
{
"title": "Caught"
},
{
"title": "Clowns Spinning Hats"
},
{
"title": "Capture of Boer Battery by British"
},
{
"title": "The Enchanted Drawing"
},
{
"title": "Feeding Sea Lions"
},
{
"title": "How to Make a Fat Wife Out of Two Lean Ones"
},
{
"title": "New Life Rescue"
},
{
"title": "New Morning Bath"
},
{
"title": "Searching Ruins on Broadway"
},
{
"title": "Sherlock Holmes Baffled"
},
{
"title": "The Tribulations of an Amateur Photographer"
},
{
"title": "Trouble in Hogan's Alley"
},
{
"title": "Two Old Sparks"
},
{
"title": "The Wonder, Ching Ling Foo"
},
{
"title": "Watermelon Contest"
},
{
"title": "Acrobats in Cairo"
},
{
"title": "An Affair of Honor"
},
{
"title": "Another Job for the Undertaker"
}
]
const container = document.getElementById("dvdContainer")
function pageTwo() {
const page = 2;
const step = 10;
const start = page * step - step;
const end = start + step;
const secondpage = movieList.slice(start, end);
// console.log(secondpage);
return secondpage;
}
function mapMovieTitle(movieTitleList) {
const element = movieTitleList.map((movie) => {
return `<span>${movie.title}</span>`
})
return element.join("<br>")
}
container.innerHTML = mapMovieTitle(pageTwo())
<div id="dvdContainer">
</div>
这是您提供的代码的翻译部分。
英文:
For mapping movies title you need to map the title with a wrapper of html element.
So, you can take reference from below snippet.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const movieList = [
{
"title": "After Dark in Central Park"
},
{
"title": "Boarding School Girls' Pajama Parade"
},
{
"title": "Buffalo Bill's Wild West Parad"
},
{
"title": "Caught"
},
{
"title": "Clowns Spinning Hats"
},
{
"title": "Capture of Boer Battery by British"
},
{
"title": "The Enchanted Drawing"
},
{
"title": "Feeding Sea Lions"
},
{
"title": "How to Make a Fat Wife Out of Two Lean Ones"
},
{
"title": "New Life Rescue"
},
{
"title": "New Morning Bath"
},
{
"title": "Searching Ruins on Broadway"
},
{
"title": "Sherlock Holmes Baffled"
},
{
"title": "The Tribulations of an Amateur Photographer"
},
{
"title": "Trouble in Hogan's Alley"
},
{
"title": "Two Old Sparks"
},
{
"title": "The Wonder, Ching Ling Foo"
},
{
"title": "Watermelon Contest"
},
{
"title": "Acrobats in Cairo"
},
{
"title": "An Affair of Honor"
},
{
"title": "Another Job for the Undertaker"
}
]
const container = document.getElementById("dvdContainer")
function pageTwo() {
const page = 2;
const step = 10;
const start = page * step - step;
const end = start + step;
const secondpage = movieList.slice(start, end);
// console.log(secondpage);
return secondpage;
}
function mapMovieTitle(movieTitleList) {
const element = movieTitleList.map((movie) => {
return `<span>${movie.title}</span>`
})
return element.join("<br>")
}
container.innerHTML = mapMovieTitle(pageTwo())
<!-- language: lang-html -->
<div id="dvdContainer">
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论