如何使用Javascript将所需功能添加到我的个人列表中?

时间:2017-08-26 15:06:31

标签: javascript jquery html css

如下所示,我有一个非常简单的列表,允许用户添加和删除项目,以及使用每个项目的3个按钮更改订单。

想要添加的

功能是删除第一项的up按钮和最后一项的down按钮。在乞讨时,你可能觉得它运行正常,但事实上我使用4行代码从当前列表中删除updown但是问题来了当用户更改项目的顺序时,添加或删除它们。 我确定你们有聪明的方法来添加这个,但我个人低于想法,不幸的是我无法让它发挥作用。我花了2个多小时来修复它,但它仍然无法正常工作。

我的想法:

第一部分:使用以下功能删除第二个up中的li按钮并将其重新添加到第一个li。另外,从 n-1 th down删除li按钮,然后重新添加到 n th li

function upButtonFixer (li1,li2){

var up = document.createElement("BUTTON");
up.className = "up"
up.textContent="up"
li1.appendChild(up); 

var upper = li2.getElementsByClassName('up')[0];
li2.removeChild(upper);
}


 function downButtonFixer (li1,li2){

 var down = document.createElement("BUTTON");
 down.className = "down"
 down.textContent="down" 
 li2.appendChild(down);

 var downer = li1.getElementsByClassName('down')[0];
 li1.removeChild(downer);
 }

第二部分: 然后我将这两个函数调用为if语句,如下所示:

ul.addEventListener('click',(event)=>{

if (event.target.tagName == "BUTTON"){
        if (event.target.className=="up"){

            **if (prevLi == ul.firstElementChild)
                  upAdderRemover(prevLi,li);**     
          }

        if (event.target.className=="down"){

               **if (li==ul.lastElementChild) 
                   downAdderRemover(li,nextLi);**


          } 
            }



const listDiv = document.querySelector("div");
const inputDescription = document.querySelector("input.description");
const pDescription = document.querySelector('p.description');
const buttonDescription = document.querySelector('button.description');
const addItemList = document.querySelector('button.adder');
const addInput = document.getElementsByClassName('addInput')[0];
const ul = document.querySelector('ul');
const removeButton = document.querySelector('button.remover');
const lis = ul.children;


function addButtons (li){

    let up = document.createElement("BUTTON");
    up.className = "up"
    up.textContent="up"
    li.appendChild(up);
    
    let down = document.createElement("BUTTON");
    down.className = "down"
    down.textContent="down"
    li.appendChild(down);
    
    let remove = document.createElement("BUTTON");
    remove.className = "remove"
    remove.textContent="remove"
    li.appendChild(remove);
}

for (var i = 0 ; i<lis.length ;i++){
    
    addButtons(lis[i]);
}

buttonDescription.addEventListener('click', ()=>{
   pDescription.textContent = inputDescription.value+':';
});  


addItemList.addEventListener('click',()=>{
    
    let newLI = document.createElement("li"); 
    newLI.textContent = addInput.value;
    addButtons(newLI)
    ul.appendChild(newLI);
    addInput.value = '';
});


removeButton.addEventListener('click', ()=>{
    
    const lastChild = document.querySelector('li:last-child');
    ul.removeChild(lastChild);
    
});



ul.addEventListener('click',(event)=>{
        
    if (event.target.tagName == "BUTTON"){
       if (event.target.className=="remove"){
       const par1 = event.target.parentNode;
       const par2 = par1.parentNode;
       par2.removeChild(par1);
   }
       if (event.target.className=="up"){
        
            const li = event.target.parentNode;
            const prevLi = li.previousElementSibling;
            if (prevLi)
                ul.insertBefore(li,prevLi);
       }
        
        else if (event.target.className=="down"){
           
            const li = event.target.parentNode;
            const nextLi = li.nextElementSibling;
            if (nextLi)
                ul.insertBefore(nextLi,li);                    

   }
   }
});

  var first = ul.firstElementChild;
  var last = ul.lastElementChild;
  var u = first.getElementsByClassName("up")[0];
  var d = last.getElementsByClassName("down")[0];
  first.removeChild(u);
  last.removeChild(d);
&#13;
@import 'https://fonts.googleapis.com/css?family=Lato:400,700';

body {
  color: #484848;
  font-family: 'Lato', sans-serif;
  padding: .45em 2.65em 3em;
  line-height: 1.5;
}
h1 {
  margin-bottom: 0;
}
h1 + p {
  font-size: 1.08em;
  color: #637a91;
  margin-top: .5em;
  margin-bottom: 2.65em;
  padding-bottom: 1.325em;
  border-bottom: 1px dotted;
}
ul {
  padding-left: 0;
  list-style: none;
}
li {
  padding: .45em .5em;
  margin-bottom: .35em;
  display: flex;
  align-items: center;
}
input,
button {
  font-size: .85em;
  padding: .65em 1em;
  border-radius: .3em;
  outline: 0;
}
input {
  border: 1px solid #dcdcdc;
  margin-right: 1em;
}
div {
  margin-top: 2.8em;
  padding: 1.5em 0 .5em;
  border-top: 1px dotted #637a91;
}
p.description,
p:nth-of-type(2) {
  font-weight: bold;
}
/* Buttons */
button {
  color: white;
  background: #508abc;
  border: solid 1px;
  border-color: rgba(0, 0, 0, .1);
  cursor: pointer;
}
button + button {
  margin-left: .5em;
}
p + button {
  background: #52bab3;
}
.list button + button {
  background: #768da3;
}
.list li button + button {
  background: #508abc;
}
li button:first-child {
  margin-left: auto;
  background: #52bab3;
}
.list li button:last-child {
  background: #768da3;
}
li button {
  font-size: .75em;
  padding: .5em .65em;
}
&#13;
<!DOCTYPE html>
<html>
    
    
    
    <head>
        
        <title>My First Website</title>
        <link rel="stylesheet" href="style.css">

    </head>
    
    
    <body>
    
        <h2>Im the H2!</h2>
        <p>Im the p!</p>        
        <div class="list">
            <p class="description">The title of the list</p>
            <input type="text" class="description">
            <button class="description">change the title!</button>
            <ul>
                <li>first </li>
                <li>second</li>
                <li>third</li>
                <li>forth</li>
                <li>fifth</li>
                <li>sixth</li>
                <li>seventh</li>
                
            </ul>
            <input type="text" class="addInput">
            <button class="adder">Add!</button>
            <button class="remover">Remove!</button>
        </div>
        
        
        
            <script src="app.js"></script>
    </body>
    
    
    
    
</html>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

我在4小时后找到了解决方案。它不是优化,但我最终使用 javascript 实施了!太开心了:))

感谢您的 CSS解决方案。请检查一下。

我们走了!

&#13;
&#13;
const toggleList = document.getElementById('toggle');
const listDiv = document.querySelector("div");
const inputDescription = document.querySelector("input.description");
const pDescription = document.querySelector('p.description');
const buttonDescription = document.querySelector('button.description');
const addItemList = document.querySelector('button.adder');
const addInput = document.getElementsByClassName('addInput')[0];
const ul = document.querySelector('ul');
const removeButton = document.querySelector('button.remover');
const lis = ul.children;


function addButtons (li){

    let up = document.createElement("BUTTON");
    up.className = "up"
    up.textContent="up"
    li.appendChild(up);
    
    let down = document.createElement("BUTTON");
    down.className = "down"
    down.textContent="down"
    li.appendChild(down);
    
    let remove = document.createElement("BUTTON");
    remove.className = "remove"
    remove.textContent="remove"
    li.appendChild(remove);
}

for (var i = 0 ; i<lis.length ;i++){
    
    addButtons(lis[i]);
}

function addup (li){
    
    
    let up = document.createElement("BUTTON");
    up.className = "up"
    up.textContent="up"
    li.appendChild(up);
    
}

buttonDescription.addEventListener('click', ()=>{
   pDescription.textContent = inputDescription.value+':';
});  


addItemList.addEventListener('click',()=>{
    
    let newLI = document.createElement("li"); 
    newLI.textContent = addInput.value;
    addButtons(newLI)
    ul.appendChild(newLI);
    addInput.value = '';
});


removeButton.addEventListener('click', ()=>{
    
    const lastChild = document.querySelector('li:last-child');
    ul.removeChild(lastChild);
    
});

function remove3Buttons(li){
    
    let x = li.firstElementChild;
    let y = li.lastElementChild;
    li.removeChild(x);
    li.removeChild(y);
}




//ul.addEventListener('mouseover', (event)=>{
//    
//    
//    if (event.target.tagName=="LI"){   
//
//        event.target.textContent=event.target.textContent.toUpperCase();
//    }    
//});
//

//ul.addEventListener('click', (event)=>{
//    
//    
//    if (event.target.tagName=="LI"){   
//        const par  = event.target.parentNode;
//        par.removeChild(event.target);
//    }    
//});

//
//ul.addEventListener('mouseout', (event)=>{
//    
//    
//    if (event.target.tagName=="LI"){   
//                 event.target.textContent=event.target.textContent.toLowerCase();
//    }    
//});
//

function upButtonFixer (li1,li2){
    
var up = document.createElement("BUTTON");
up.className = "up"
up.textContent="up"
li1.appendChild(up); 
    
var upper = li2.getElementsByTagName('BUTTON')[0];
li2.removeChild(upper);
}


function downButtonFixer (li1,li2){
    
var down = document.createElement("BUTTON");
down.className = "down"
down.textContent="down" 
li2.appendChild(down);

var downer = li1.getElementsByTagName('BUTTON')[1];
li1.removeChild(downer);
}





ul.addEventListener('click',(event)=>{
        
    if (event.target.tagName == "BUTTON"){
       if (event.target.className=="remove"){
       const par1 = event.target.parentNode;
       const par2 = par1.parentNode;
       par2.removeChild(par1);
   }
       if (event.target.className=="up"){
            
           const first = ul.firstElementChild;
           const second = first.nextElementSibling;
           const last = ul.lastElementChild;
            const secondEnd = last.previousElementSibling;
            const li = event.target.parentNode;
            const prevLi = li.previousElementSibling;
           
            if (prevLi)
                ul.insertBefore(li,prevLi);
        
           
           
           
            if (li == last){
                var z0 = secondEnd.firstElementChild;
                var z1 = secondEnd.lastElementChild;
                var z2 = z1.previousElementSibling;
                remove3Buttons(li);
                addButtons(li);
                secondEnd.removeChild(z2);
            }
           
           
           if (li == second){
                var a1 = li.firstElementChild;
                li.removeChild(a1);
                remove3Buttons(prevLi);
                addButtons(prevLi);
            }}
        
        if (event.target.className=="down"){
            
            const last = ul.lastElementChild;
            const secondEnd = last.previousElementSibling;
            const first = ul.firstElementChild;
            const second = first.nextElementSibling;
            const li = event.target.parentNode;
            const nextLi = li.nextElementSibling;
            if (nextLi)
                ul.insertBefore(nextLi,li);
            
            if (li == secondEnd){
                var z0 = li.firstElementChild;
                var z1 = li.lastElementChild;
                var z2 = z1.previousElementSibling;
                remove3Buttons(nextLi);
                addButtons(nextLi);
                li.removeChild(z2)
            }    
            
            
            if (li == first){
                var z0 = second.firstElementChild;
                var z1 = second.lastElementChild;
                var z2 = z1.previousElementSibling;
                remove3Buttons(li);
                addButtons(li);
                second.removeChild(z0);
            }
                        

   }
   }
});


var first = ul.firstElementChild;
var last = ul.lastElementChild;
var u = first.getElementsByClassName("up")[0];
var d = last.getElementsByClassName("down")[0];
first.removeChild(u);
last.removeChild(d);



    
&#13;
@import 'https://fonts.googleapis.com/css?family=Lato:400,700';

body {
  color: #484848;
  font-family: 'Lato', sans-serif;
  padding: .45em 2.65em 3em;
  line-height: 1.5;
}
h1 {
  margin-bottom: 0;
}
h1 + p {
  font-size: 1.08em;
  color: #637a91;
  margin-top: .5em;
  margin-bottom: 2.65em;
  padding-bottom: 1.325em;
  border-bottom: 1px dotted;
}
ul {
  padding-left: 0;
  list-style: none;
}
li {
  padding: .45em .5em;
  margin-bottom: .35em;
  display: flex;
  align-items: center;
}
input,
button {
  font-size: .85em;
  padding: .65em 1em;
  border-radius: .3em;
  outline: 0;
}
input {
  border: 1px solid #dcdcdc;
  margin-right: 1em;
}
div {
  margin-top: 2.8em;
  padding: 1.5em 0 .5em;
  border-top: 1px dotted #637a91;
}
p.description,
p:nth-of-type(2) {
  font-weight: bold;
}
/* Buttons */
button {
  color: white;
  background: #508abc;
  border: solid 1px;
  border-color: rgba(0, 0, 0, .1);
  cursor: pointer;
}
button + button {
  margin-left: .5em;
}
p + button {
  background: #52bab3;
}
.list button + button {
  background: #768da3;
}
.list li button + button {
  background: #508abc;
}
li button:first-child {
  margin-left: auto;
  background: #52bab3;
}
.list li button:last-child {
  background: #768da3;
}
li button {
  font-size: .75em;
  padding: .5em .65em;
}
&#13;
<!DOCTYPE html>
<html>
    
    
    
    <head>
        
        <title>My First Website</title>
        <link rel="stylesheet" href="style.css">

    </head>
    
    
    <body>
    
        <h2>Im the H2!</h2>
        <p>Im the p!</p>
        <button id="toggle">Hide list!</button> 
        
        <div class="list">
            <p class="description">The title of the list</p>
            <input type="text" class="description">
            <button class="description">change the title!</button>
            <ul>
                <li>first </li>
                <li>second</li>
                <li>third</li>
                <li>forth</li>
                <li>fifth</li>
                <li>sixth</li>
                <li>seventh</li>
                
            </ul>
            <input type="text" class="addInput">
            <button class="adder">Add!</button>
            <button class="remover">Remove!</button>
        </div>
        
        
        
            <script src="app.js"></script>
    </body>
    
    
    
    
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

可以使用CSS完成此操作。我注意到它已将第一组按钮向左移动,但总体而言,这可以满足您的要求。

我确信您可以使用css解决小问题。

我删除了删除第一个up和最后down按钮的javascript,并使用css隐藏了第一个up和最后一个down按钮以下......

ul li:first-child .up{ 
    display:none;
}
ul li:last-child .down{ 
    display:none;
}

const listDiv = document.querySelector("div");
const inputDescription = document.querySelector("input.description");
const pDescription = document.querySelector('p.description');
const buttonDescription = document.querySelector('button.description');
const addItemList = document.querySelector('button.adder');
const addInput = document.getElementsByClassName('addInput')[0];
const ul = document.querySelector('ul');
const removeButton = document.querySelector('button.remover');
const lis = ul.children;


function addButtons(li) {

  let up = document.createElement("BUTTON");
  up.className = "up"
  up.textContent = "up"
  li.appendChild(up);

  let down = document.createElement("BUTTON");
  down.className = "down"
  down.textContent = "down"
  li.appendChild(down);

  let remove = document.createElement("BUTTON");
  remove.className = "remove"
  remove.textContent = "remove"
  li.appendChild(remove);
}

for (var i = 0; i < lis.length; i++) {

  addButtons(lis[i]);
}

buttonDescription.addEventListener('click', () => {
  pDescription.textContent = inputDescription.value + ':';
});


addItemList.addEventListener('click', () => {

  let newLI = document.createElement("li");
  newLI.textContent = addInput.value;
  addButtons(newLI)
  ul.appendChild(newLI);
  addInput.value = '';
});


removeButton.addEventListener('click', () => {

  const lastChild = document.querySelector('li:last-child');
  ul.removeChild(lastChild);

});



ul.addEventListener('click', (event) => {

  if (event.target.tagName == "BUTTON") {
    if (event.target.className == "remove") {
      const par1 = event.target.parentNode;
      const par2 = par1.parentNode;
      par2.removeChild(par1);
    }
    if (event.target.className == "up") {

      const li = event.target.parentNode;
      const prevLi = li.previousElementSibling;
      if (prevLi)
        ul.insertBefore(li, prevLi);
    } else if (event.target.className == "down") {

      const li = event.target.parentNode;
      const nextLi = li.nextElementSibling;
      if (nextLi)
        ul.insertBefore(nextLi, li);

    }
  }
});

//Deleted source code to remove first & last up/down buttons
@import 'https://fonts.googleapis.com/css?family=Lato:400,700';
body {
  color: #484848;
  font-family: 'Lato', sans-serif;
  padding: .45em 2.65em 3em;
  line-height: 1.5;
}

h1 {
  margin-bottom: 0;
}

h1+p {
  font-size: 1.08em;
  color: #637a91;
  margin-top: .5em;
  margin-bottom: 2.65em;
  padding-bottom: 1.325em;
  border-bottom: 1px dotted;
}

ul {
  padding-left: 0;
  list-style: none;
}

li {
  padding: .45em .5em;
  margin-bottom: .35em;
  display: flex;
  align-items: center;
}

input,
button {
  font-size: .85em;
  padding: .65em 1em;
  border-radius: .3em;
  outline: 0;
}

input {
  border: 1px solid #dcdcdc;
  margin-right: 1em;
}

div {
  margin-top: 2.8em;
  padding: 1.5em 0 .5em;
  border-top: 1px dotted #637a91;
}

p.description,
p:nth-of-type(2) {
  font-weight: bold;
}


/* Buttons */
/* Added.....*/
ul li:first-child .up{ 
    display:none;
}
ul li:last-child .down{ 
    display:none;
}
button {
  color: white;
  background: #508abc;
  border: solid 1px;
  border-color: rgba(0, 0, 0, .1);
  cursor: pointer;
}

button+button {
  margin-left: .5em;
}

p+button {
  background: #52bab3;
}

.list button+button {
  background: #768da3;
}

.list li button+button {
  background: #508abc;
}

li button:first-child {
  margin-left: auto;
  background: #52bab3;
}

.list li button:last-child {
  background: #768da3;
}

li button {
  font-size: .75em;
  padding: .5em .65em;
}
<h2>Im the H2!</h2>
<p>Im the p!</p>
<div class="list">
  <p class="description">The title of the list</p>
  <input type="text" class="description">
  <button class="description">change the title!</button>
  <ul>
    <li>first </li>
    <li>second</li>
    <li>third</li>
    <li>forth</li>
    <li>fifth</li>
    <li>sixth</li>
    <li>seventh</li>

  </ul>
  <input type="text" class="addInput">
  <button class="adder">Add!</button>
  <button class="remover">Remove!</button>
</div>

</html>

如果您对我所做的编辑有任何疑问,请在下面留言,我会尽快给您回复。

我希望这会有所帮助。快乐的编码!

  

编辑:按钮对齐修正

如果您想快速修复这些按钮,可以将css替换为以下

ul li:first-child .up{ 
    visibility:hidden;
}

答案 2 :(得分:0)

这将是满足您需求的CSS解决方案,此方法将是最简单的。

我正在使用:last-child:first-child来显示/隐藏向上和向下按钮。

.list>ul>li:first-child>button.up{
  display:none;
}
.list>ul>li:first-child>button.down{
  margin-left: auto;
}
.list>ul>li:last-child>button.down{
  display:none;
}

&#13;
&#13;
const listDiv = document.querySelector("div");
const inputDescription = document.querySelector("input.description");
const pDescription = document.querySelector('p.description');
const buttonDescription = document.querySelector('button.description');
const addItemList = document.querySelector('button.adder');
const addInput = document.getElementsByClassName('addInput')[0];
const ul = document.querySelector('ul');
const removeButton = document.querySelector('button.remover');
const lis = ul.children;


function addButtons (li){

    let up = document.createElement("BUTTON");
    up.className = "up"
    up.textContent="up"
    li.appendChild(up);
    
    let down = document.createElement("BUTTON");
    down.className = "down"
    down.textContent="down"
    li.appendChild(down);
    
    let remove = document.createElement("BUTTON");
    remove.className = "remove"
    remove.textContent="remove"
    li.appendChild(remove);
}

for (var i = 0 ; i<lis.length ;i++){
    
    addButtons(lis[i]);
}

buttonDescription.addEventListener('click', ()=>{
   pDescription.textContent = inputDescription.value+':';
});  


addItemList.addEventListener('click',()=>{
    
    let newLI = document.createElement("li"); 
    newLI.textContent = addInput.value;
    addButtons(newLI)
    ul.appendChild(newLI);
    addInput.value = '';
});


removeButton.addEventListener('click', ()=>{
    
    const lastChild = document.querySelector('li:last-child');
    ul.removeChild(lastChild);
    
});



ul.addEventListener('click',(event)=>{
        
    if (event.target.tagName == "BUTTON"){
       if (event.target.className=="remove"){
       const par1 = event.target.parentNode;
       const par2 = par1.parentNode;
       par2.removeChild(par1);
   }
       if (event.target.className=="up"){
        
            const li = event.target.parentNode;
            const prevLi = li.previousElementSibling;
            if (prevLi)
                ul.insertBefore(li,prevLi);
       }
        
        else if (event.target.className=="down"){
           
            const li = event.target.parentNode;
            const nextLi = li.nextElementSibling;
            if (nextLi)
                ul.insertBefore(nextLi,li);                    

   }
   }
});
&#13;
@import 'https://fonts.googleapis.com/css?family=Lato:400,700';

body {
  color: #484848;
  font-family: 'Lato', sans-serif;
  padding: .45em 2.65em 3em;
  line-height: 1.5;
}
h1 {
  margin-bottom: 0;
}
.list>ul>li:first-child>button.up{
  display:none;
}
.list>ul>li:first-child>button.down{
  margin-left: auto;
}
.list>ul>li:last-child>button.down{
  display:none;
}
h1 + p {
  font-size: 1.08em;
  color: #637a91;
  margin-top: .5em;
  margin-bottom: 2.65em;
  padding-bottom: 1.325em;
  border-bottom: 1px dotted;
}
ul {
  padding-left: 0;
  list-style: none;
}
li {
  padding: .45em .5em;
  margin-bottom: .35em;
  display: flex;
  align-items: center;
}
input,
button {
  font-size: .85em;
  padding: .65em 1em;
  border-radius: .3em;
  outline: 0;
}
input {
  border: 1px solid #dcdcdc;
  margin-right: 1em;
}
div {
  margin-top: 2.8em;
  padding: 1.5em 0 .5em;
  border-top: 1px dotted #637a91;
}
p.description,
p:nth-of-type(2) {
  font-weight: bold;
}
/* Buttons */
button {
  color: white;
  background: #508abc;
  border: solid 1px;
  border-color: rgba(0, 0, 0, .1);
  cursor: pointer;
}
button + button {
  margin-left: .5em;
}
p + button {
  background: #52bab3;
}
.list button + button {
  background: #768da3;
}
.list li button + button {
  background: #508abc;
}
li button:first-child {
  margin-left: auto;
  background: #52bab3;
}
.list li button:last-child {
  background: #768da3;
}
li button {
  font-size: .75em;
  padding: .5em .65em;
}
&#13;
<!DOCTYPE html>
<html>
    
    
    
    <head>
        
        <title>My First Website</title>
        <link rel="stylesheet" href="style.css">

    </head>
    
    
    <body>
    
        <h2>Im the H2!</h2>
        <p>Im the p!</p>        
        <div class="list">
            <p class="description">The title of the list</p>
            <input type="text" class="description">
            <button class="description">change the title!</button>
            <ul>
                <li>first </li>
                <li>second</li>
                <li>third</li>
                <li>forth</li>
                <li>fifth</li>
                <li>sixth</li>
                <li>seventh</li>
                
            </ul>
            <input type="text" class="addInput">
            <button class="adder">Add!</button>
            <button class="remover">Remove!</button>
        </div>
        
        
        
            <script src="app.js"></script>
    </body>
    
    
    
    
</html>
&#13;
&#13;
&#13;