当我按下div“用户”给我这个用户的帖子,但当我改变到另一个用户强制删除自动为旧用户的帖子,并给我一个新用户的帖子,不幸的是,它显示新用户的帖子底部的旧用户的帖子。
删除旧的职位,并给我带来新的职位上相同的部门。
此函数用于创建元素帖子:
function getPost(userID){
fetch('https://jsonplaceholder.typicode.com/posts?userId='+ userID)
.then((response) =>{
if (response.ok) {
return response.json()
}
})
.then((posts) => {
for(let post of posts){
const parentPosts = document.querySelector(".infos")
const orginalPost = document.querySelector("#demo")
console.log(orginalPost);
// orginalPost.style.display="none";
// //orginalPost.innerHTML='';
let newPost = document.createElement('div')
newPost.innerHTML = orginalPost.innerHTML;
newPost.classList.remove("orginal")
newPost.className = 'info';
// // //console.log('new post ',newPost);
let postHead = document.querySelector(".postHead")
let postP = document.querySelector(".postP")
postHead.innerHTML = post.title;
postP.innerHTML = post.body;
// // //const parentPosts = document.querySelector(".infos")
parentPosts.appendChild(newPost)
remove(parentPosts)
}
return
});
}
这个功能应该是工作的删除旧职位
function remove(element) {
let elementLength = element.children.length;
console.log("element.children.length >> "+elementLength)
if (element > 0) {
element.firstChild.remove()
//element.removeChild(element.lastChild);
console.log("element.children.length from loop >> "+ elementLength)
}
}
这些都是代码
<html>
<head>
<style>
body{
background: linear-gradient(120DEG,#c0a397,#ebe6b4);
}
.conterner{
width: 100%;
display: flex;
flex-direction: row;
}
.users{
margin: 2px;
background: white;
width: 30%;
height: 100%;
}
.infos{
margin: 2px;
background: white;
width: 70%;
}
.user{
border-radius: 65px;
background-color: rgba(187, 200, 200, 0.315);
margin: 0 auto;
cursor: pointer;
}
.user:hover,
:active{
border: rgb(224, 152, 132) 2px solid;
}
h3{
padding: 1px;
margin-left: 25px;
}
.info{
border-radius: 23px;
background-color: rgba(187, 200, 200, 0.331);
margin: 8px;
padding: 2px ;
padding-left: 20px;
}
.line{
width: 97%;
margin:0;
padding: 0;
border-bottom: 1px solid black;
/* position: relative; */
}
.dispaly{
display: none;
}
.orginal{
display: none;
}
</style>
</head>
<body>
<main>
<div class="conterner">
<div class="users " >
<div style="margin: 0 a;" id="parUser">
<div class="user" id="user" >
<h3 id="name"> Ahmed </h3>
<h3 id="email"> Ahmed.ali@gmail.com </h3>
</div>
</div>
</div>
<div class="info orginal" id="demo">
<h5 class="postHead" > post post postpost</h5>
<div class="line"></div>
<p class="postP"> post post postpost</p>
</div>
<div class="infos">
<div class="info">
<h5 class="postHead" > post post postpost</h5>
<div class="line"></div>
<p class="postP"> post post postpost</p>
</div>
</div>
</div>
</main>
<script>
function getUsers(userID){
fetch('https://jsonplaceholder.typicode.com/users')
.then((response) =>{
if (response.ok) {
return response.json()
}
})
.then((users) => {
for(let user of users){
// console.log(user)
const orginalDiv = document.querySelector("#user");
orginalDiv.style.display ='none';
const newDiv = document.createElement('div')
newDiv.innerHTML= orginalDiv.innerHTML;
newDiv.className="user";
const nameH = document.querySelector('#name')
nameH.innerHTML = user.name;
const emailH = document.querySelector('#email')
emailH.innerHTML = user.email;
const parentDiv = document.getElementById("parUser")
parentDiv.appendChild(newDiv);
console.log(newDiv);
newDiv.addEventListener("click", ()=>userEvent(user.id))
console.log(user.id)
const parentPosts = document.querySelector(".infos")
}
return
})
// orderElement(user)
}
function getPost(userID){
fetch('https://jsonplaceholder.typicode.com/posts?userId='+ userID)
.then((response) =>{
if (response.ok) {
return response.json()
}
})
.then((posts) => {
for(let post of posts){
const parentPosts = document.querySelector(".infos")
const orginalPost = document.querySelector("#demo")
console.log(orginalPost);
// orginalPost.style.display="none";
// //orginalPost.innerHTML='';
let newPost = document.createElement('div')
newPost.innerHTML = orginalPost.innerHTML;
newPost.classList.remove("orginal")
newPost.className = 'info';
// // //console.log('new post ',newPost);
let postHead = document.querySelector(".postHead")
let postP = document.querySelector(".postP")
postHead.innerHTML = post.title;
postP.innerHTML = post.body;
// // //const parentPosts = document.querySelector(".infos")
parentPosts.appendChild(newPost)
remove(parentPosts)
}
return
});
}
function remove(element) {
let elementLength = element.children.length;
console.log("element.children.length >> "+elementLength)
if (element > 0) {
element.firstChild.remove()
//element.removeChild(element.lastChild);
console.log("element.children.length from loop >> "+ elementLength)
}
}
getUsers()
getPost()
function userEvent(id) {
getPost(id);
}
</script>
</body>
</html>
1条答案
按热度按时间ghg1uchk1#
这个功能应该是工作的删除旧职位
函数
remove
包含以下块:假设
element
很可能是HTMLElement
,那么它永远不会是true
,即使它是null
。您应该使用长度变量
elementLength
,它是您之前准备好的,包含一个整数:虽然这就总结了为什么您的元素从未被删除,但我建议在添加
newPost
之前删除parentPosts
的内容。这里是一个调整后的片段。