gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:16:08 26 4
gpt4 key购买 nike

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

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

我的想法:

第一部分: 制作以下功能,用于从第二个 li 中删除 up 按钮并将其重新添加到第一个 li。此外,从 n-1th li 中删除 down 按钮并重新添加到 nth 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com