gpt4 book ai didi

javascript - 使用 Javascript 循环创建多个 HTML 元素

转载 作者:行者123 更新时间:2023-11-29 16:04:44 27 4
gpt4 key购买 nike

我想使用 javascript 循环来创建多个 HTML 包装器元素并将 JSON 响应 API 数据插入到某些元素(图像、标题、url 等...)中。

这是我需要逐行处理的东西吗?

<a class="scoreboard-video-outer-link" href="">
<div class="scoreboard-video--wrapper">
<div class="scoreboard-video--thumbnail">
<img src="http://via.placeholder.com/350x150">
</div>
<div class="scoreboard-video--info">
<div class="scoreboard-video--title">Pelicans @ Bulls Postgame: E'Twaun Moore 10-8-17</div>
</div>
</div>
</a>

我正在尝试:

var link = document.createElement('a');
document.getElementsByTagName("a")[0].setAttribute("class", "scoreboard-video-outer-link");
document.getElementsByTagName("a")[0].setAttribute("url", "google.com");
mainWrapper.appendChild(link);

var videoWrapper= document.createElement('div');
document.getElementsByTagName("div")[0].setAttribute("class", "scoreboard-video-outer-link");
link.appendChild(videoWrapper);

var videoThumbnailWrapper = document.createElement('div');
document.getElementsByTagName("div")[0].setAttribute("class", "scoreboard-video--thumbnail");
videoWrapper.appendChild(videoThumbnailWrapper);

var videoImage = document.createElement('img');
document.getElementsByTagName("img")[0].setAttribute("src", "url-of-image-from-api");
videoThumbnailWrapper.appendChild(videoImage);

然后我基本上对所有嵌套的 HTML 元素重复该过程。

  • 创建 A 标签
  • 为 A 标签创建类和 href 属性
  • 将类名和 url 附加到属性
  • 将 A 标签附加到主包装器

  • 创建DIV

  • 为 DIV 创建类属性
  • 将 DIV 附加到新附加的 A 标签

如果您能告诉我最好的方法来完成我在这里要解释的内容,我将不胜感激?看起来会变得很乱。

最佳答案

这是我的答案。它被记录下来。为了查看代码片段中的效果,您必须进入开发人员控制台以检查包装器元素或查看您的开发人员控制台日志。

我们基本上创建了一些辅助方法来轻松创建元素并将它们附加到 DOM - 它实际上并不像看起来那么难。这也应该让您可以轻松地将 JSON 检索对象作为属性附加到您的元素!

这是一个基本版本,让您了解正在发生的事情以及如何使用它

//create element function

function create(tagName, props) {
return Object.assign(document.createElement(tagName), (props || {}));
}

//append child function

function ac(p, c) {
if (c) p.appendChild(c);
return p;
}

//example:
//get wrapper div
let mainWrapper = document.getElementById("mainWrapper");

//create link and div
let link = create("a", { href:"google.com" });
let div = create("div", { id: "myDiv" });

//add link as a child to div, add the result to mainWrapper
ac(mainWrapper, ac(div, link));

//create element function

function create(tagName, props) {
return Object.assign(document.createElement(tagName), (props || {}));
}

//append child function

function ac(p, c) {
if (c) p.appendChild(c);
return p;
}

//example:
//get wrapper div
let mainWrapper = document.getElementById("mainWrapper");

//create link and div
let link = create("a", { href:"google.com", textContent: "this text is a Link in the div" });
let div = create("div", { id: "myDiv", textContent: "this text is in the div! " });

//add link as a child to div, add the result to mainWrapper
ac(mainWrapper, ac(div, link));
div {
border: 3px solid black;
padding: 5px;

}
<div id="mainWrapper"></div>

这里是如何使用更完整的注释代码来具体执行您的要求。

//get main wrapper
let mainWrapper = document.getElementById("mainWrapper");

//make a function to easily create elements
//function takes a tagName and an optional object for property values
//using Object.assign we can make tailored elements quickly.

function create(tagName, props) {
return Object.assign(document.createElement(tagName), (props || {}));
}


//document.appendChild is great except
//it doesn't offer easy stackability
//The reason for this is that it always returns the appended child element
//we create a function that appends from Parent to Child
//and returns the compiled element(The Parent).
//Since we are ALWAYS returning the parent(regardles of if the child is specified)
//we can recursively call this function to great effect
//(you'll see this further down)
function ac(p, c) {
if (c) p.appendChild(c);
return p;
}

//these are the elements you wanted to append
//notice how easy it is to make them!

//FYI when adding classes directly to an HTMLElement
//the property to assign a value to is className -- NOT class
//this is a common mistake, so no big deal!

var link = create("a", {
className: "scoreboard-video-outer-link",
url: "google.com"
});

var videoWrapper = create("div", {
className: "scoreboard-video-outer-link"
});

var videoThumbnailWrapper = create("div", {
className: "scoreboard-video--thumbnail"
});

var videoImage = create("img", {
src: "url-of-image-from-api"
});

//here's where the recursion comes in:
ac(mainWrapper, ac(link, ac(videoWrapper, ac(videoThumbnailWrapper, videoImage))));

//keep in mind that it might be easiest to read the ac functions backwards
//the logic is this:

//Append videoImage to videoThumbnailWrapper
//Append (videoImage+videoThumbnailWrapper) to videoWrapper
//Append (videoWrapper+videoImage+videoThumbnailWrapper) to link
//Append (link+videoWrapper+videoImage+videoThumbnailWrapper) to mainWrapper

let mainWrapper = document.getElementById('mainWrapper');

function create(tagName, props) {
return Object.assign(document.createElement(tagName), (props || {}));
}

function ac(p, c) {
if (c) p.appendChild(c);
return p;
}

var link = create("a", {
className: "scoreboard-video-outer-link",
url: "google.com"
});

var videoWrapper = create("div", {
className: "scoreboard-video-outer-link"
});

var videoThumbnailWrapper = create("div", {
className: "scoreboard-video--thumbnail"
});



var videoImage = create("img", {
src: "url-of-image-from-api"
});

ac(mainWrapper, ac(link, ac(videoWrapper, ac(videoThumbnailWrapper, videoImage))));
//pretty fancy.
//This is just to show the output in the log,
//feel free to just open up the developer console and look at the mainWrapper element.

console.dir(mainWrapper);
<div id="mainWrapper"></div>

关于javascript - 使用 Javascript 循环创建多个 HTML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46738333/

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