如何将HTML列表放入JavaScript数组中?
我想创建一个类似以下内容的按钮: 到目前为止,我已经做了以下工作: =========================================================================如何将HTML列表放入JavaScript数组中?,javascript,html,Javascript,Html,我想创建一个类似以下内容的按钮: 到目前为止,我已经做了以下工作: ========================================================================= <button id = "button" onclick = "random_element()">click here</button> <h1 style = "col
<button id = "button" onclick = "random_element()">click
here</button>
<h1 style = "color:pink;"> Project Title</h1>
<p id = "ZZZ" style = "font-size: 14px; color:
#a8bcff;"></p>
<script>
var zzz = document.getElementById('ZZZ');
var arr = ["aaa", "bbb", "ccc", "ddd"];
// how do we substitute these strings with HTML lists in arr?
function random_element() {
zzz.innerHTML = arr[Math.floor(Math.random() * arr.length)];
}
</script>
点击
在这里
项目名称
var zzz=document.getElementById('zzz');
var arr=[“aaa”、“bbb”、“ccc”、“ddd”];
//如何在arr中用HTML列表替换这些字符串?
函数random_element(){
zzz.innerHTML=arr[Math.floor(Math.random()*arr.length)];
}
视频1随机选择/点击按钮显示
视频2随机选择/点击按钮显示
视频3随机选择/点击按钮显示
视频4随机选择/点击按钮显示
=========================================================================
<button id = "button" onclick = "random_element()">click
here</button>
<h1 style = "color:pink;"> Project Title</h1>
<p id = "ZZZ" style = "font-size: 14px; color:
#a8bcff;"></p>
<script>
var zzz = document.getElementById('ZZZ');
var arr = ["aaa", "bbb", "ccc", "ddd"];
// how do we substitute these strings with HTML lists in arr?
function random_element() {
zzz.innerHTML = arr[Math.floor(Math.random() * arr.length)];
}
</script>
我了解如何以文本的形式生成随机元素。但是我不知道如何创建一个包含所有iframe的html列表的JS数组
在JavaScript数组中的
索引到数组中的同一索引来关联它们显示
var-arr=[“aaa”、“bbb”、“ccc”、“ddd”];
const lis=document.queryselectoral(“#list li”);
lis.forEach(功能(el,i){
const btn=document.createElement('button');
btn.textContent=arr[i];
btn.addEventListener('click',function(){
//对数组值执行一些操作
控制台清除()
log('Btn索引:',i',数组值:',arr[i])
})
el.儿童(btn);
});代码>
- 一个
- 两个
- 三
- 三
为什么我们必须添加多个按钮?我们的目标是有一个按钮,可以获取和显示一个随机建议您使用搜索引擎并研究所使用的每种方法的内容。逐行给出解释是乏味的,使用的代码非常标准,接近于在元素集合上循环和添加事件侦听器