英文:
Text value from an array displayed on a div
问题
我已经漫游了一阵子,但无法使其工作。我从localStorage中的数组中获取了一个文本值,但在简单的部分上失败了,无法将该值显示在一个div中。
这是我的代码:
function loadLastSell() {
var contacts = JSON.parse(localStorage.getItem("f7Contacts")) || {};
for (var k = 0; k < contacts.length; k++) {
var Sellings = contacts[k].sellings;
}console.log(Sellings);
var sellings = Sellings;
for (var x = 0; x < sellings.length; x++) {
var lselling = document.getElementById("lselling");
var lSelling = sellings[x].vdate;
}console.log(lSelling);
}
<div class="item-title">Fecha de confección:</div>
<div class="item-inner">
<div class="item-subtitle" id="lselling"></div>
</div>
这是调试结果:
但我无法将文本值显示在div中,如图所示:
我想要的是获取数组的最后一个元素并显示在div上。我的代码正在运行,但我无法将元素显示在div上。我需要获取sellings中的最后一个对象,并在#lselling上显示vdate的值。就这样。console.log(lSelling); 显示 19/03/2023 05:34 AM,这就是我想要显示的。我已经在一个contact中,现在我需要获取该联系人的最后一个selling并显示vdate的值。
我的代码有什么问题?
更新
对于未来的提问者,我通过将函数转为 async 来解决了这个问题,就像这样:
async function loadLastSell() {
var contacts = JSON.parse(localStorage.getItem("f7Contacts")) || {};
for (var k = 0; k < contacts.length; k++) {
var Sellings = contacts[k].sellings;
}
var sellings = Sellings;
for (var x = 0; x < sellings.length; x++) {
await 2;
lselling = document.getElementById("lselling")
lSelling = sellings[x].vdate;
}
lselling.innerHTML = lSelling
}
现在 vdate 的最后一个 selling 的值在 div 上正确显示了。
英文:
I have being wandering around this but can't make it work. I am getting a text value from an array on localStorage but I am failing with the easy part, display this value on a div.
This is my code:
javascript
function loadLastSell() {
var contacts = JSON.parse(localStorage.getItem("f7Contacts")) || {};
for (var k = 0; k < contacts.length; k++) {
var Sellings = contacts[k].sellings;
}console.log(Sellings);
var sellings = Sellings;
for (var x = 0; x < sellings.length; x++) {
var lselling = document.getElementById("lselling");
var lSelling = sellings[x].vdate;
}console.log(lSelling);
}
html
<div class="item-title">Fecha de confección:</div>
<div class="item-inner">
<div class="item-subtitle" id="lselling"></div>
</div>
This is the debuging result:
But I can't manage to display the text value on the div as shown in this image:
What I want is to get the last element of the array and display it on the div. My code is working but I can't manage to diplay the element on the div. I need to get the last object withing sellings and display the value of vdate on #lselling. Just that. console.log(lSelling); is showing 19/03/2023 05:34 AM, and this is what I want to be displayed there. I'm already within a contact, now I need to get the last selling of this contact and display the value of vdate.
What's wrong within my code?
UPDATE
For future askers I solved it turning the function to async just like this:
async function loadLastSell() {
var contacts = JSON.parse(localStorage.getItem("f7Contacts")) || {};
for (var k = 0; k < contacts.length; k++) {
var Sellings = contacts[k].sellings;
}
var sellings = Sellings;
for (var x = 0; x < sellings.length; x++) {
await 2;
lselling = document.getElementById("lselling")
lSelling = sellings[x].vdate;
}
lselling.innerHTML = lSelling
}
Now the value of vdate last selling ise properly displayed on the div.
答案1
得分: 0
根据我的理解,您已经在变量“lSelling”中存储了文本值。
而“lselling”变量保留了您想要显示此文本的 div 元素。
要将文本放入 div 元素中,您可以使用“innerHTML”作为选项:
// ...
for (var x = 0; x < sellings.length; x++) {
var lselling = document.getElementById("lselling");
var lSelling = sellings[x].vdate;
}console.log(lSelling);
// 将您的变量放入 div 的 innerHTML 中:
lselling.innerHTML = lSelling
}
实际上,您不需要循环,可以这样编写:
const isellingElm = document.getElementById("lselling");
const lastSelling = sellings[sellings.length - 1];
isellingElm.innerHTML = lastSelling.vdate;
更新:
要使 JS 代码正常工作,必须在加载 HTML 内容后执行。
最简单的方法是将“




评论