如何增加和减少点击数并将其存储在本地存储中?

时间:2018-09-08 12:19:59

标签: javascript dom ecmascript-6 local-storage

这是Reddit应用程序的示例。我想在单击“ upvote”或“ downvote”按钮时增加或减少数字,并且其值应存储在本地存储中...谢谢;

HTML标记:

<div class="submit-box">
    <form id="myForm">
        <h2>Add a Link</h2>
        <h3>Title:</h3>
        <input type="text" placeholder="Enter title" id="siteName">
        <h3>Link:</h3>
        <input type="text" id="siteUrl" placeholder="Enter URL Link">
        <button class="btn" >Submit Link</button>
    </form>
</div>

<div class="container">
    <div class="content-box" id="content">
        // Code dynamically generated from javascript...  
    </div>
</div>

JavaScript代码:

let form = document.getElementById('myForm');
let content = document.querySelector('#content');


function addIt(e) {
    e.preventDefault();
    let siteName = document.getElementById('siteName').value;
    let siteUrl = document.getElementById('siteUrl').value;

    content.innerHTML += `
    <div class="row">
      <div class="points col-md-4">
          <p class="num">0</p>
          <p class="point">Points</p>
       </div>
       <div class="matter col-md-6">
         <h2>${siteName}</h2>
         <p class="lead">${siteUrl}</p>
         <button class="upvote">upvote</button>
         <button class="downvote">downvote</button>
       </div>
     </div>
      `

    votes();
}


function votes() {
    let upvotes = Array.from(document.querySelectorAll('.upvote'));
    let downvotes = Array.from(document.querySelectorAll('.downvote'));

   upvotes.forEach(upvote => upvote.addEventListener('click', up));
   downvotes.forEach(downvote => downvote.addEventListener('click', down));

   function up() {
     //...
   }

   function down() {
     //...
   }

}

结果输出:

enter image description here

1 个答案:

答案 0 :(得分:1)

首先,找到每个按钮的要点,即

let point = this.parentNode.parentNode.querySelector(".num");

因为它位于按钮的父div的父div中,并且它是第一个适合.num的元素。

此后,递增或递减innerHTML

point.innerHTML = parseInt(point.innerHTML)+1

let form = document.getElementById('myForm');
let content = document.querySelector('#content');

form.addEventListener("submit", addIt);

function addIt(e) {
  e.preventDefault();
  let siteName = document.getElementById('siteName').value;
  let siteUrl = document.getElementById('siteUrl').value;

  content.innerHTML += `
    <div class="row rating">
      <div class="points col-md-4">
          <p class="num">0</p>
          <p class="point">Points</p>
       </div>
       <div class="matter col-md-6">
         <h2>${siteName}</h2>
         <p class="lead">${siteUrl}</p>
         <button class="upvote">upvote</button>
         <button class="downvote">downvote</button>
       </div>
     </div>
      `

  votes();
}


function votes() {
  let upvotes = Array.from(document.querySelectorAll('.upvote'));
  let downvotes = Array.from(document.querySelectorAll('.downvote'));

  upvotes.forEach(upvote => upvote.addEventListener('click', up));
  downvotes.forEach(downvote => downvote.addEventListener('click', down));

  function up() {
    let point = this.parentNode.parentNode.querySelector(".num");
    point.innerHTML = parseInt(point.innerHTML)+1
  }

  function down() {
    let point = this.parentNode.parentNode.querySelector(".num");
    point.innerHTML = parseInt(point.innerHTML)-1
  }

}
.rating {
  border: 1px solid red;
}
<div class="submit-box">
  <form id="myForm">
    <h2>Add a Link</h2>
    <h3>Title:</h3>
    <input type="text" placeholder="Enter title" id="siteName">
    <h3>Link:</h3>
    <input type="text" id="siteUrl" placeholder="Enter URL Link">
    <button class="btn" type="submit">Submit Link</button>
  </form>
</div>

<div class="container">
  <div class="content-box" id="content">
    // Code dynamically generated from javascript...
  </div>
</div>

我添加了红色边框,以便您可以查看哪些元素属于哪个网站。

要将其保存到localStorage,可以从网站制作一个JSON Array并将其保存。当您添加新网站时,请从localStorage获取该阵列并将新网站添加到其中。加载时获得相同的效果,并为它们每个运行addIt。

let form = document.getElementById('myForm');
let content = document.querySelector('#content');
form.addEventListener("submit", addIt);


let websiteArr = [];

try {
  websiteArr = JSON.parse(localStorage.getItem("websites"));
} catch {
  // no websites in localStorage
}

if (websiteArr.length != 0) {
  websiteArr.forEach(web => {
    addIt(!0, {name:web.name, url:web.url, points:web.points});
  });
}


function addIt(e, options) {
  e && e.preventDefault(); // only run preventDefault when addIt is called in the submit handler of the form
  let siteName = document.getElementById('siteName').value;
  let siteUrl = document.getElementById('siteUrl').value;
  let points = 0;
  
  if(options) {
    siteName = options.name;
    siteUrl = options.url;
    points = options.points;
  }

  content.innerHTML += `
    <div class="row rating">
      <div class="points col-md-4">
          <p class="num">${points}</p>
          <p class="point">Points</p>
       </div>
       <div class="matter col-md-6">
         <h2>${siteName}</h2>
         <p class="lead">${siteUrl}</p>
         <button class="upvote">upvote</button>
         <button class="downvote">downvote</button>
       </div>
     </div>
      `

  votes();
}

function saveArray() {
  localStorage.setItem("websites", JSON.stringify(websiteArr));
}


function votes() {
  let upvotes = Array.from(document.querySelectorAll('.upvote'));
  let downvotes = Array.from(document.querySelectorAll('.downvote'));

  upvotes.forEach(upvote => upvote.addEventListener('click', up));
  downvotes.forEach(downvote => downvote.addEventListener('click', down));

  function up() {
    let point = this.parentNode.parentNode.querySelector(".num");
    point.innerHTML = parseInt(point.innerHTML)+1
    let url = this.parentNode.querySelector("p").innerHTML;
    websiteArr.forEach(web => (web.url === url) && web.points++);
    saveArray();
  }

  function down() {
    let point = this.parentNode.parentNode.querySelector(".num");
    point.innerHTML = parseInt(point.innerHTML)-1
    let url = this.parentNode.querySelector("p").innerHTML;
    websiteArr.forEach(web => (web.url === url) && web.points--);
    saveArray();
  }

}
.rating {
  border: 1px solid red;
}
<div class="submit-box">
  <form id="myForm">
    <h2>Add a Link</h2>
    <h3>Title:</h3>
    <input type="text" placeholder="Enter title" id="siteName">
    <h3>Link:</h3>
    <input type="text" id="siteUrl" placeholder="Enter URL Link">
    <button class="btn" type="submit">Submit Link</button>
  </form>
</div>

<div class="container">
  <div class="content-box" id="content">
    // Code dynamically generated from javascript...
  </div>
</div>