正如您在下面看到的,我有一个非常简单的列表,该列表允许用户添加和删除项目以及使用每个项目的3个按钮更改顺序。

要添加的功能是删除第一个项目的up按钮和最后一个项目的down按钮。在开始时,您可能会觉得它工作正常,但是实际上我只用了4行代码从当前列表中删除了updown,但是问题出在用户更改项目的顺序,添加或删除它们时。
我敢肯定,你们有聪明的方法来添加它,但是我个人有以下想法,但是不幸的是我无法使它起作用。我花了2个多小时来修复它,但仍然无法正常工作。

我的点子:

第一部分:进行以下功能,从第二个up移除li按钮并将其重新添加到第一个li。另外,从第n-1个down移除li按钮,然后重新添加到第n个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);

@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;
}

<!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>

最佳答案

4小时后我找到了解决方案。它没有经过优化,但是我终于使用javascript实现了!很高兴 :)

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

开始了!



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);



    

@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;
}

<!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>

关于javascript - 如何使用Javascript将所需的功能添加到我的个人列表中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45896778/

10-12 12:29
查看更多