为什么我的函数在控制台中有效,但无法返回它?

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

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:

&lt;button id=&quot;next&quot; onclick=&quot;nextTwo()&quot;&gt;2&lt;/button&gt;

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(&#39;x_list&#39;).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 &#39;x_list&#39; to display the objects
    const container = document.getElementById(&#39;x_list&#39;);
    
    // Clear the container before adding the new content
    container.innerHTML = &#39;&#39;;
    
    // Convert the array to an HTML string and append it to the container
    nextobj.forEach(obj =&gt; {
        const item = document.createElement(&#39;div&#39;);
        item.textContent = obj.title; // Assuming each object has a &#39;title&#39; 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 = [
{
&quot;title&quot;: &quot;After Dark in Central Park&quot;
},
{
&quot;title&quot;: &quot;Boarding School Girls&#39; Pajama Parade&quot;
},
{
&quot;title&quot;: &quot;Buffalo Bill&#39;s Wild West Parad&quot;
},
{
&quot;title&quot;: &quot;Caught&quot;
},
{
&quot;title&quot;: &quot;Clowns Spinning Hats&quot;
},
{
&quot;title&quot;: &quot;Capture of Boer Battery by British&quot;
},
{
&quot;title&quot;: &quot;The Enchanted Drawing&quot;
},
{
&quot;title&quot;: &quot;Feeding Sea Lions&quot;
},
{
&quot;title&quot;: &quot;How to Make a Fat Wife Out of Two Lean Ones&quot;
},
{
&quot;title&quot;: &quot;New Life Rescue&quot;
},
{
&quot;title&quot;: &quot;New Morning Bath&quot;
},
{
&quot;title&quot;: &quot;Searching Ruins on Broadway&quot;
},
{
&quot;title&quot;: &quot;Sherlock Holmes Baffled&quot;
},
{
&quot;title&quot;: &quot;The Tribulations of an Amateur Photographer&quot;
},
{
&quot;title&quot;: &quot;Trouble in Hogan&#39;s Alley&quot;
},
{
&quot;title&quot;: &quot;Two Old Sparks&quot;
},
{
&quot;title&quot;: &quot;The Wonder, Ching Ling Foo&quot;
},
{
&quot;title&quot;: &quot;Watermelon Contest&quot;
},
{
&quot;title&quot;: &quot;Acrobats in Cairo&quot;
},
{
&quot;title&quot;: &quot;An Affair of Honor&quot;
},
{
&quot;title&quot;: &quot;Another Job for the Undertaker&quot;
}
]
const container = document.getElementById(&quot;dvdContainer&quot;)
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) =&gt; {
return `&lt;span&gt;${movie.title}&lt;/span&gt;`
})
return element.join(&quot;&lt;br&gt;&quot;)
}
container.innerHTML = mapMovieTitle(pageTwo())

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

&lt;div id=&quot;dvdContainer&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月23日 21:12:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748419.html
匿名

发表评论

匿名网友

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

确定