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

标签 javascript html css

我有 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/

相关文章:

javascript - select2 列表未填充

html - 在图像中间居中 Bootstrap div

HTML CSS ASP.NET

php - 如何根据当前页面/网址获得 ="selected"类

javascript - 收到错误数据后清空 kendo UI 中的网格

Javascript iframe 速度问题

javascript - 为什么在 AngularJs 中 http.delete 时我不能传递参数?

css - rake Assets :预编译抛出 Sass::SyntaxError: "*/"后无效的 CSS

javascript - 如何动态地将相同大小和位置的图像放置在另一个 div 上?

jquery - 当我访问我的页面时,我的分享按钮不会立即出现,当它出现时我们看到页面正在调整