gpt4 book ai didi

javascript - 动态附加内容时出现问题,包括内联 javascript 和样式

转载 作者:行者123 更新时间:2023-11-28 00:35:32 24 4
gpt4 key购买 nike

我有一个列表的 html 标记

<ul>
<a id="catalogPlaceholder"></a> <!-- this is the placeholder to append new items -->

<a class="sidebarListItem" href="#link">
<object type="deleteLinkOnHover"><a class="deleteItem" href="javascript:deleteItemFromList()">Delete</a></object>
<div style="width:30%;float:left; background: none"><img class="rounded-circle" src="#image" alt="Product image" width="100%"></div>
<div style="float:left;padding-left:10px;width:60%;background: none"><strong class="text-muted d-block mb-2"><h5>#title</h5></strong></div>
</a>
<a class="sidebarListItem" href="#link">
<object type="deleteLinkOnHover"><a class="deleteItem" href="javascript:deleteItemFromList()">Delete</a></object>
<div style="width:30%;float:left; background: none"><img class="rounded-circle" src="#image" alt="Product image" width="100%"></div>
<div style="float:left;padding-left:10px;width:60%;background: none"><strong class="text-muted d-block mb-2"><h5>#title</h5></strong></div>
</a>
<a class="sidebarListItem" href="#link">
<object type="deleteLinkOnHover"><a class="deleteItem" href="javascript:deleteItemFromList()">Delete</a></object>
<div style="width:30%;float:left; background: none"><img class="rounded-circle" src="#image" alt="Product image" width="100%"></div>
<div style="float:left;padding-left:10px;width:60%;background: none"><strong class="text-muted d-block mb-2"><h5>#title</h5></strong></div>
</a>
</ul>

看起来像this image

并想像这样使用 javascript 添加新项目

function relateNewCatalog(id, image, title) {
var a = document.createElement('a');
a.classname = "sidebarListItem";
a.id = "sidebarItemCatalog-" + id;
a.href = "../?module=catalogDetails&idcatalog=" + id;
a.style = "padding:10px";

var html = '<object type="deleteLinkOnHover"><a class="deleteItem" href="javascript:deleteItemFromList(\'sidebarItemCatalog-' + id + '\')">Delete</a></object>';
html += '<div style="width:30%;float:left; background: none"><img class="rounded-circle" src="../uploads/images/' + image + '" alt="Product image" width="100%"></div>';
html += '<div style="float:left;padding-left:10px;width:60%;background: none"><strong class="text-muted d-block mb-2"><h5>' + title + '</h5></strong></div>';

a.innerHTML = html;
var placeholder = document.getElementById("catalogPlaceholder");
placeholder.append(a);
}

但是当我在 this image 中这样做时,标记似乎被破坏了(参见填充和悬停效果)。我尝试了 clear:bothinsertBefore 而不是 appendChild 但没有成功。我添加的项目越多,它们堆叠的越多,就像在 last image 中一样

感谢您的宝贵时间!

最佳答案

您可以使用 <template>并分离数据并查看如下

let data = [
{ id: 1, title: 'Product 1', image: "https://picsum.photos/id/10/50" },
{ id: 2, title: 'Product 2', image: "https://picsum.photos/id/20/50" },
{ id: 3, title: 'Product 3', image: "https://picsum.photos/id/30/50" }
]

function refresh() {
let r='', inj = (str, obj) => str.replace(/\${(.*?)}/g, (x,g)=> obj[g]);
for(let it of data) r += inj(item.innerHTML,it);
catalogPlaceholder.innerHTML = r;
}

function relateNewCatalog() {
data.unshift({
id: Math.max(...data.map(x=>x.id),0)+1,
title: `Product ${data.length+1}`,
image: `https://picsum.photos/id/${Math.random()*300|0}/50`
});
refresh();
}

function deleteItemFromList(id) {
data = data.filter(x=> x.id!=id);
refresh();
}

refresh();
.sidebarListItem {display: flex}
h5 {margin:0}
<button onclick="relateNewCatalog()">Add</button>
<ul id="catalogPlaceholder" ></ul>

<template id="item" >
<li>
<a class="sidebarListItem" href="#link">
<object type="deleteLinkOnHover">
<a class="deleteItem" onclick="deleteItemFromList(${id})">Delete</a>
</object>
<div class="imgBox">
<img class="rounded-circle" src="${image}" alt="Product image" >
</div>
<div class="titleBox"><strong class="text-muted d-block mb-2">
<h5>${title}</h5>
</strong></div>
</a>
</li>
</template>

关于javascript - 动态附加内容时出现问题,包括内联 javascript 和样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56454355/

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