gpt4 book ai didi

javascript - 将随机选择的值显示到类内的段落标签

转载 作者:太空宇宙 更新时间:2023-11-04 06:50:41 24 4
gpt4 key购买 nike

我有 5 个类名为 box 的盒子和一个包含 12 个名为 items 的值的数组,我使用 rvalue() 随机选择它而不重复功能。

我想将随机选择的标签值显示到<p>里面标签

我应该如何最好地做到这一点?

这是我的代码:

var array2=[];
rvalue();
var items = [
{ label: '1', url: '1.png' },
{ label: '2', url: '2.png' },
{ label: '3', url: '3.png' },
{ label: '4', url: '4.png' },
{ label: '5', url: '5.png' },
{ label: '6', url: '6.png' },
{ label: '7', url: '7.png' },
{ label: '8', url: '8.png' },
{ label: '9', url: '9.png' },
{ label: '10', url: '10.png'},
{ label: '11', url: '11.png'},
{ label: '12', url: '12.png'}];


array2 = items.slice();


var item;


function rvalue()
{
ptags = document.querySelectorAll('[name="values"]');

for (var index = 0; index <3; index++)
{
randomIndex = Math.floor(Math.random() * array2.length);
item = array2.splice(randomIndex, 1);

ptags[index].textContent = item.label;

ptags[index].dataset.itemIndex = randomIndex;
}

}
body {
overflow: hidden;
}


.heading{
margin-left:20%;
margin-right:20%;
margin-top:-2%;
}


.box {
width: calc(15.4% - 4px);
display: inline-block;
border-radius: 5px;
border: 2px solid #333;
border: #000 border-color: #e6e600;
margin: -2px;
background-color:#0F6;
}

.box {
height: 15vh;
display: inline-flex;
align-items: center;
justify-content: center
}

#container {
white-space:nowrap;
text-align:center;
border:px solid #CC0000;
margin:2px;
margin-right:2px;

}


.box p {
font-size: calc(2vw + 10px);
}
p{
font: "Courier New", Courier, monospace;
font-size:30px;

color: #005ce6;
text-align: center;
}

.text {
padding: 20px;
margin:7 px;
margin-top:10px;
color:white;
font-weight:bold;
text-align:center;
}

body{
background-size: 100vw 100vh;
}
<div id="container">

<div class="box" id="10"><p name="values"></p></div>
<div class="box" id="11"><p name="values"></p></div>
<div class="box" id="12"><p name="values"></p></div>
<div class="box" id="13"><p name="values"></p></div>
<div class="box" id="14"><p name="values"></p></div>

</div>

最佳答案

array2.splice(randomIndex, 1); 

返回包含 1 个对象的数组,因此您代码中的项是对象数组使用项[0]

var array2=[];

var items = [
{ label: '1', url: '1.png' },
{ label: '2', url: '2.png' },
{ label: '3', url: '3.png' },
{ label: '4', url: '4.png' },
{ label: '5', url: '5.png' },
{ label: '6', url: '6.png' },
{ label: '7', url: '7.png' },
{ label: '8', url: '8.png' },
{ label: '9', url: '9.png' },
{ label: '10', url: '10.png'},
{ label: '11', url: '11.png'},
{ label: '12', url: '12.png'}];


array2 = items.slice();


var item;


function rvalue()
{
ptags = document.querySelectorAll('.box');

for (var index = 0; index <3; index++)
{
randomIndex = Math.floor(Math.random() * array2.length);
item = array2.splice(randomIndex, 1);

ptags[index].children[0].textContent = item[0].label;

// ptags[index].dataset.itemIndex = randomIndex;
}

}

rvalue()
body {
overflow: hidden;
}


.heading{
margin-left:20%;
margin-right:20%;
margin-top:-2%;
}


.box {
width: calc(15.4% - 4px);
display: inline-block;
border-radius: 5px;
border: 2px solid #333;
border: #000 border-color: #e6e600;
margin: -2px;
background-color:#0F6;
}

.box {
height: 15vh;
display: inline-flex;
align-items: center;
justify-content: center
}

#container {
white-space:nowrap;
text-align:center;
border:px solid #CC0000;
margin:2px;
margin-right:2px;

}


.box p {
font-size: calc(2vw + 10px);
}
p{
font: "Courier New", Courier, monospace;
font-size:30px;

color: #005ce6;
text-align: center;
}

.text {
padding: 20px;
margin:7 px;
margin-top:10px;
color:white;
font-weight:bold;
text-align:center;
}

body{
background-size: 100vw 100vh;
}
<div id="container">

<div class="box" id="10"><p name="values"></p></div>
<div class="box" id="11"><p name="values"></p></div>
<div class="box" id="12"><p name="values"></p></div>
<div class="box" id="13"><p name="values"></p></div>
<div class="box" id="14"><p name="values"></p></div>

</div>

关于javascript - 将随机选择的值显示到类内的段落标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52902598/

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