首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript [object HTMLCollection]

Javascript [object HTMLCollection]
EN

Stack Overflow用户
提问于 2020-01-25 02:32:11
回答 2查看 3.5K关注 0票数 0

使用Javascript,我正在创建一个列表网站(作为一个附带项目)。

我使用代码:

代码语言:javascript
复制
var myNodelist = document.getElementsByTagName("LI");

这很管用,但是

代码语言:javascript
复制
localStorage.setItem("myNodelist", document.getElementsByTagName("LI"));

没有,它只是返回[object HTMLCollection]。有人知道为什么吗?

代码语言:javascript
复制
// Create a "close" button and append it to each list item
var myNodelist = document.getElementsByTagName("LI");
var i;
for (i = 0; i < myNodelist.length; i++) {
  var span = document.createElement("SPAN");
  var txt = document.createTextNode("\u00D7");
  span.className = "close";
  span.appendChild(txt);
  myNodelist[i].appendChild(span);
}

// Click on a close button to hide the current list item
var close = document.getElementsByClassName("close");
var i;
for (i = 0; i < close.length; i++) {
  close[i].onclick = function() {
    var div = this.parentElement;
    div.style.display = "none";
  }
}

// Add a "checked" symbol when clicking on a list item
var list = document.querySelector('ul');
list.addEventListener('click', function(ev) {
  if (ev.target.tagName === 'LI') {
    ev.target.classList.toggle('checked');
  }
}, false);

// Create a new list item when clicking on the "Add" button
function newElement() {
  var li = document.createElement("li");
  var inputValue = document.getElementById("myInput").value;
  var t = document.createTextNode(inputValue);
  li.appendChild(t);
  if (inputValue === '') {
    alert("You must write something!");
  } else {
    document.getElementById("myUL").appendChild(li);
  }
  document.getElementById("myInput").value = "";

  var span = document.createElement("SPAN");
  var txt = document.createTextNode("\u00D7");
  span.className = "close";
  span.appendChild(txt);
  li.appendChild(span);

  for (i = 0; i < close.length; i++) {
    close[i].onclick = function() {
      var div = this.parentElement;
      div.style.display = "none";
    }
  }
}
代码语言:javascript
复制
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    body {
      margin: 0;
      min-width: 250px;
    }
    /* Include the padding and border in an element's total width and height */
    
    * {
      box-sizing: border-box;
    }
    /* Remove margins and padding from the list */
    
    ul {
      margin: 0;
      padding: 0;
    }
    /* Style the list items */
    
    ul li {
      cursor: pointer;
      position: relative;
      padding: 12px 8px 12px 40px;
      list-style-type: none;
      background: #eee;
      font-size: 18px;
      transition: 0.2s;
      /* make the list items unselectable */
      -webkit-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: none;
    }
    /* Set all odd list items to a different color (zebra-stripes) */
    
    ul li:nth-child(odd) {
      background: #f9f9f9;
    }
    /* Darker background-color on hover */
    
    ul li:hover {
      background: #ddd;
    }
    /* When clicked on, add a background color and strike out text */
    
    ul li.checked {
      background: #888;
      color: #fff;
      text-decoration: line-through;
    }
    /* Add a "checked" mark when clicked on */
    
    ul li.checked::before {
      content: '';
      position: absolute;
      border-color: #fff;
      border-style: solid;
      border-width: 0 2px 2px 0;
      top: 10px;
      left: 16px;
      transform: rotate(45deg);
      height: 15px;
      width: 7px;
    }
    /* Style the close button */
    
    .close {
      position: absolute;
      right: 0;
      top: 0;
      padding: 12px 16px 12px 16px;
    }
    
    .close:hover {
      background-color: #168e00;
      color: white;
    }
    /* Style the header */
    
    .header {
      background-color: #168e00;
      padding: 30px 40px;
      color: white;
      text-align: center;
    }
    /* Clear floats after the header */
    
    .header:after {
      content: "";
      display: table;
      clear: both;
    }
    /* Style the input */
    
    input {
      margin: 0;
      border: none;
      border-radius: 0;
      width: 75%;
      padding: 10px;
      float: left;
      font-size: 16px;
    }
    /* Style the "Add" button */
    
    .addBtn {
      padding: 10px;
      width: 25%;
      background: #d9d9d9;
      color: #555;
      float: left;
      text-align: center;
      font-size: 16px;
      cursor: pointer;
      transition: 0.3s;
      border-radius: 0;
    }
    
    .addBtn:hover {
      background-color: #bbb;
    }
  </style>
</head>

<body>

  <div id="myDIV" class="header">
    <h2 style="margin:5px">Groceries:</h2>
    <input type="text" id="myInput" placeholder="Title...">
    <span onclick="newElement()" class="addBtn">Add</span>
  </div>

  <ul id="myUL">

  </ul>


</body>

</html>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-25 02:46:34

如果在var Lists = document.getElementsByTagName("List");中编写console.log(list),您将看到需要的元素列表,对吗?

console.log也以同样的方式显示数组,数组的每个元素都被打印到日志中,而不必编写for()循环。

但是,如果要使用数组,则必须编写for循环

对象HTMLCollection

意味着有一个HTML对象数组,为了处理必须使用for循环或数组方法的数据,所以在将其存储在本地存储中之前

尝试使用以下对象集合创建数组

代码语言:javascript
复制
var arr = Array.from(htmlCollection); 

然后按原样保存或转换为json字符串。

代码语言:javascript
复制
 var myJsonString = JSON.stringify(arr);

我相信这会有帮助

票数 1
EN

Stack Overflow用户

发布于 2020-01-25 02:48:26

首先:如果使用id从dom中查找DOM元素,情况会更好。第二:您需要使用.innerText来访问列表项的文本内容。

代码语言:javascript
复制
<ul>
  <li id="item">hello</li>
</ul>

localStorage.setItem("myNodelist",document.getElementById("item").innerText);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59905871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档