Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
91 views
in Technique[技术] by (71.8m points)

javascript - Conflict between the same function which is once called outside function and then inside another function

The HTML, CSS, and JavaScript code I added here works perfectly but I have a very minor problem I.e, for ideal case, if I click on the name of item list, it should get a strike through. The code outcome also matches my expectation but only with the default list name. As soon as I add a new list item, the default list items are not getting strike though any more when they get a click. I guess it is due to conflict of strikeThrought() function (I may be wrong).

document.querySelector("button").addEventListener("click", function() {
  if (document.querySelector("input").value.length > 0) {
    input();
  }
})


document.querySelector("input").addEventListener("keypress", function(event) {
  if (document.querySelector("input").value.length > 0 && event.keyCode === 13) {
    input();
  }
})


deleteList();
strikeThrought();


function input() {
  var input = " " + document.querySelector("input").value;
  var li = document.querySelector("ul").appendChild(document.createElement("li"));
  var button = li.appendChild(document.createElement("button"));
  button.innerHTML = "Delete";
  var span = li.appendChild(document.createElement("span"));
  span.innerHTML = input;
  document.querySelector("input").value = "";

  deleteList();
  strikeThrought();
}


function deleteList() {
  for (var i = 0; i < document.querySelectorAll("li button").length; i++) {
    document.querySelectorAll("li button")[i].addEventListener("click", function() {
      for (var j = 0; j < document.querySelectorAll("li").length; j++) {
        this.parentNode.remove()
      }
    })
  }
}


function strikeThrought() {
  for (var i = 0; i < document.querySelectorAll("span").length; i++) {
    document.querySelectorAll("span")[i].addEventListener("click", function() {
      this.classList.toggle("done");
    })
  }
}
li{
  padding: 3px;
}
p{
  margin: 0;
}
.coolTitle {
  text-align: center;
  font-family: 'Oswald', Helvetica, sans-serif;
  font-size: 40px;
  transform: skewY(-10deg);
  letter-spacing: 4px;
  word-spacing: -8px;
  color: tomato;
  text-shadow:
    -1px -1px 0 firebrick,
    -2px -2px 0 firebrick,
    -3px -3px 0 firebrick,
    -4px -4px 0 firebrick,
    -5px -5px 0 firebrick,
    -6px -6px 0 firebrick,
    -7px -7px 0 firebrick,
    -8px -8px 0 firebrick,
    -30px 20px 40px dimgrey;
}

.done {
  text-decoration: line-through;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <h1>Shopping List</h1>
  <p>Get it done today</p>
  <input type="text" placeholder="Type to enter">
  <button type="button">Enter</button>
  <ul>
    <li class="li0"><button class="toggle-button">Delete</button>  <span>Notebook</span></li>
    <li class="li1"><button class="toggle-button">Delete</button>  <span>Jello</span></li>
    <li class="li2"><button class="toggle-button">Delete</button>  <span>Rice</span></li>
    <li class="li3"><button class="toggle-button">Delete</button>  <span>Spinach</span></li>
    <li class="li4"><button class="toggle-button">Delete</button>  <span>Birthday Cake</span></li>
    <li class="li5"><button class="toggle-button">Delete</button>  <span>Candles</span></li>
  </ul>
  <script src="index.js" charset="utf-8"></script>
</body>
</html>
question from:https://stackoverflow.com/questions/65937435/conflict-between-the-same-function-which-is-once-called-outside-function-and-the

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should delegate the clicks to a higher element (document in my example below) and not try to add the event handlers every time a change is made.

const inputButton = document.querySelector("button");
const inputField = document.querySelector("input")

inputButton.addEventListener("click", function() {
  if (inputField.value.length > 0) {
    input();
  }
})


inputField.addEventListener("keypress", function(event) {
  if (inputField.value.length > 0 && event.keyCode === 13) {
    input();
  }
})

document.addEventListener("click", function(event) {
  if(event.target.nodeName == "SPAN")
    event.target.classList.toggle("done");
  else if(event.target.nodeName == "BUTTON" && event.target.classList.contains("toggle-button")) {
    event.target.parentElement.remove();
  }
})



function input() {
  var input = " " + document.querySelector("input").value;
  var li = document.querySelector("ul").appendChild(document.createElement("li"));
  var button = li.appendChild(document.createElement("button"));
  button.innerHTML = "Delete";
  button.classList.add("toggle-button");
  var span = li.appendChild(document.createElement("span"));
  span.innerHTML = input;
  document.querySelector("input").value = "";
}
li{
  padding: 3px;
}
p{
  margin: 0;
}
.coolTitle {
  text-align: center;
  font-family: 'Oswald', Helvetica, sans-serif;
  font-size: 40px;
  transform: skewY(-10deg);
  letter-spacing: 4px;
  word-spacing: -8px;
  color: tomato;
  text-shadow:
    -1px -1px 0 firebrick,
    -2px -2px 0 firebrick,
    -3px -3px 0 firebrick,
    -4px -4px 0 firebrick,
    -5px -5px 0 firebrick,
    -6px -6px 0 firebrick,
    -7px -7px 0 firebrick,
    -8px -8px 0 firebrick,
    -30px 20px 40px dimgrey;
}

.done {
  text-decoration: line-through;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <h1>Shopping List</h1>
  <p>Get it done today</p>
  <input type="text" placeholder="Type to enter">
  <button type="button">Enter</button>
  <ul>
    <li class="li0"><button class="toggle-button">Delete</button>  <span>Notebook</span></li>
    <li class="li1"><button class="toggle-button">Delete</button>  <span>Jello</span></li>
    <li class="li2"><button class="toggle-button">Delete</button>  <span>Rice</span></li>
    <li class="li3"><button class="toggle-button">Delete</button>  <span>Spinach</span></li>
    <li class="li4"><button class="toggle-button">Delete</button>  <span>Birthday Cake</span></li>
    <li class="li5"><button class="toggle-button">Delete</button>  <span>Candles</span></li>
  </ul>
  <script src="index.js" charset="utf-8"></script>
</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...