[英]How to show element from function which returns array in Javascript
So I should get announcement's i element's x.所以我应该得到公告的 i 元素的 x。 In the end of the code, I've added loop which have to set left and top properties to the pin.
在代码的最后,我添加了循环,它必须为引脚设置 left 和 top 属性。 Please help me:) Here is my code:
请帮助我:) 这是我的代码:
var announcement = function () {
var result = [];
for (var i = 0; i < 8; i++) {
result.push({
"location": {
"x": Math.floor(Math.random() * 1200) + 1,
"y": Math.floor(Math.random() * (630 - 130) ) + 130
}
})
}
return result;
}
var map = document.querySelector(".map");
var pinTemplate = document.querySelector("#pin").content.querySelector(".map__pin");
for (var i = 0; i < announcement().length - 1; i++) {
var pin = pinTemplate.cloneNode(true);
pin.setAttribute("style", "left: " + announcement()[i].x + "top: " + announcement()[i].y);
document.querySelector(".map__pins").appendChild(pin);
console.log(announcement()[i].x);
}
Try this:尝试这个:
You can do like this to get element and display in div or any element you want.您可以这样做来获取元素并显示在 div 或您想要的任何元素中。
Please see the working example.请参阅工作示例。
var announcement = function () { var result = []; for (var i = 0; i < 8; i++) { result.push({ "location": { "x": Math.floor(Math.random() * 1200) + 1, "y": Math.floor(Math.random() * (630 - 130) ) + 130 } }) } return result; } var node = document.createElement("div"); announcement().forEach(function (element) { var textnode = document.createTextNode('left: '+element.location.x+' - top: '+element.location.y+' '); node.appendChild(textnode); document.getElementById("myList").appendChild(node); })
<div id="myList"> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.