数组中的元素无法识别

时间:2020-06-02 03:34:54

标签: javascript arrays for-loop dom appendchild

    var userInput = prompt('Which fruit would you like?');
      var fruitList = ["banana", 'strawberries', "mango"];

      for(var i=0; i<fruitList.length; i++);
        if(userInput == fruitList[i]){
          alert("Sorry, we are out of " + userInput);
        }else{
          var userFruit = document.createElement('li');
          userFruit.textContent = userInput;
          var fruits = document.getElementById('list');
          fruits.appendChild(userFruit);
        }
    <div id="list"></div>

for循环似乎不起作用,因为它会将用户输入添加到列表中,但无法识别数组中的元素。我没有在此处添加的列表具有id="list",并且具有数组中的三个元素。谢谢!

  var userInput = prompt('Which fruit would you like?');
  var fruitList = ["banana", 'strawberries', "mango"];

  for(var i=0; i<fruitList.length; i++);
    if(userInput == fruitList[i]){
      alert("Sorry, we are out of " + userInput);
    }else{
      var userFruit = document.createElement('li');
      userFruit.textContent = userInput;
      var fruits = document.getElementById('list');
      fruits.appendChild(userFruit);
    }

3 个答案:

答案 0 :(得分:1)

我对您的代码进行了更合理的编辑。

它与您的期望相似吗?

response = get('https://www.storytel.com/in/en/categories/8-History?pageNumber=1', headers = headers)
soup = BeautifulSoup(response.text,"html.parser")
books_div = soup.find_all('div', class_='gridItem')
for container in books_div:
    name = container.find('div',class_='gridBookTitle').a.text
    titles.append(name)

    b_id = container.div.find('a').get('href')
    b_id = b_id[-6:-1]
    book_id.append(b_id)

    for i,t in zip(book_id,titles):
        response1 = get('https://www.storytel.com/in/en/books/'+i+'-'+t, headers = headers)
        soup = BeautifulSoup(response1.text,"html.parser")
        tab_div = soup.find('div', class_='bookDetailDesc')
        a_name = tab_div.find('span', class_:'expandAuthorName').text
        author.append(a_name)
        b_rating = tab_div.find('span', id='dennisBookRating').text
        rating.append(b_rating) 
steps:
  - powershell: |
    'Start-Process -FilePath "C:\Windows\System32\cmd.exe" -verb runas -ArgumentList {/k echo "hello"}' 
  - powershell: |
    'Start-Process -FilePath "C:\Windows\System32\cmd.exe" -verb runas -ArgumentList {/k echo "world"}'

答案 1 :(得分:1)

您应该将大括号添加到for循环中...不需要,但是本质上比较整齐。您的问题是语法,因为在for循环的结尾处有一个半冒号终止了循环。

更改此 -> for(var i=0; i<fruitList.length; i++);

为此 -> for(var i=0; i<fruitList.length; i++){for(var i=0; i<fruitList.length; i++)

在编辑时注意:将变量移至for循环外并分配了innerHTML,也可以将textContent应用于变量外循环。然后在for循环中,将创建的li项附加到ul父项。

enter image description here

const userInput = prompt('Which fruit would you like?');
const fruitList = ["banana", 'strawberries', "mango"];
const fruits = document.getElementById('list');
const userFruit = document.createElement('li')
userFruit.innerHTML = userInput;

for (let i = 0; i < fruitList.length; i++) {
  if (userInput === fruitList[i]) {
    alert("Sorry, we are out of " + userInput);
  } else {
    fruits.append(userFruit);
  }
}  
<ul id="list"></ul>

答案 2 :(得分:0)

这是因为它会将您的条目与数组中的所有水果进行比较,因此只要没有太多,它将添加到列表中。您可以改用includes方法来检查数组中元素的存在。

  var userInput = prompt('Which fruit would you like?');

  var fruitList = ["banana", 'strawberries', "mango"];

  if(fruitList.includes(userInput)) {
    alert("Sorry, we are out of " + userInput);
  }
  else {
    var userFruit = document.createElement('li');
    userFruit.textContent = userInput;
    var fruits = document.getElementById('list');
    fruits.appendChild(userFruit);
  }
 <div id="list"></div>

相关问题