尝试在Javascript中为表行的2个值之间创建随机数
我正在使用HTML CSS和Javascript制作一个简单的天气网站。 我搜索了很多不同的问题,但我不知道如何创建两个东西尝试在Javascript中为表行的2个值之间创建随机数,javascript,html,Javascript,Html,我正在使用HTML CSS和Javascript制作一个简单的天气网站。 我搜索了很多不同的问题,但我不知道如何创建两个东西 对于表中的温度行,我希望在每次刷新页面时为温度生成并插入随机数。id是“mNum”和“maNum” 在每一页刷新,我想随机显示天气图片的 我想同时使用Javascript实现这两个功能,但我就是找不到并想不出如何实现 如果有人能给我指出正确的方向那就太好了!提前谢谢 我的HTML表格: <table class="week" id="weer"> &l
<table class="week" id="weer">
<tr class="days" id="header" >
<th class="r1">Vandaag</th>
<th class ="r2">Morgen</th>
<th class = "r3">Overmorgen</th>
<th class="r4">Daarna</th>
</tr>
<tr class="picto" id="pics">
<td class="r1"><img src="picto/rain%20(1).png" alt="regen" height="40"></td>
<td class="r2"><img src="picto/rainsun.png" alt="regen" height="40"></td>
<td class="r3"><img src="picto/cloudy.png" alt="wolk" height="40"></td>
<td class="r4"><img src="picto/sun.png" alt="zon" height="40"></td>
</tr>
<tr class="min" id="mTemp">
<td class="r1">Min. Temperatuur</td>
<td class="r2">Min. Temperatuur</td>
<td class="r3">Min. Temperatuur</td>
<td class="r4">Min. Temperatuur</td>
</tr>
<tr class="mintemp" id="mNum">
<td class="r1">5°</td>
<td class="r2">7°</td>
<td class="r3">9°</td>
<td class="r4">11°</td>
</tr>
<tr class="max" id="maTemp">
<td class="r1" >Max. Temperatuur</td>
<td class="r2">Max. Temperatuur</td>
<td class="r3">Max. Temperatuur</td>
<td class="r4">Max. Temperatuur</td>
</tr>
<tr class="maxtemp" id="maNum">
<td class="r1">16°</td>
<td class="r2">19°</td>
<td class="r3">21°</td>
<td class="r4">25°</td>
</tr>
<tr class="wind" id="wind">
<td colspan="4">Wind is niet van toepassing deze week</td>
</tr>
</table>
我的气象表的外观图片:
提前谢谢 您可以通过innerHtml和QuerySelector添加值:
//获取max temperature div的所有子项,并将一些html添加到
document.querySelector(“#maNum”).children.map((元素)=>{
element.innerHTML=`${randomMax()}°`;
});
//获取min temperature div的所有子项,并将一些html添加到
document.querySelector(“#mNum”).children.map((元素)=>{
element.innerHTML=`${randomMin()}°`;
});
//将图片添加到列表中
常数picsList=[
'',
'',
'',
''
];
//将随机图像html添加到图片上的元素
document.querySelector(“#pics”).children.map((元素)=>{
element.innerHTML=picsList[Math.floor(Math.random()*picsList.length];//数组的ramdom索引
});
我用来在表中获取温度随机数的代码:
function randomMax(){
var min = 9;
var max = 21;
return Math.floor(Math.random() * (+max - +min)) + +min;
}
document.getElementById('n5').innerHTML = randomMax() + '°';
document.getElementById('n6').innerHTML = randomMax() + '°';
document.getElementById('n7').innerHTML = randomMax() + '°';
document.getElementById('n8').innerHTML = randomMax() + '°';
function randomMin(){
var min = -6;
var max = 8;
return Math.floor(Math.random() * (+max - +min)) + +min;
}
document.getElementById('n1').innerHTML = randomMin() + '°';
document.getElementById('n2').innerHTML = randomMin() + '°';
document.getElementById('n3').innerHTML = randomMin() + '°';
document.getElementById('n4').innerHTML = randomMin() + '°';
这段代码让我在所有需要它的表格框中得到随机数,我得到了每个具有不同ID的框,当刷新页面时,值会发生变化
我用来在表中获取随机图像的代码:
var picsList = [
'picto/rain%20(1).png',
'picto/rainsun.png',
'picto/cloudy.png',
'picto/sun.png'
];
function randImg() {
const size = picsList.length;
document.getElementById('i1').src = picsList[Math.floor(size * Math.random())];
document.getElementById('i2').src = picsList[Math.floor(size * Math.random())];
document.getElementById('i3').src = picsList[Math.floor(size * Math.random())];
document.getElementById('i4').src = picsList[Math.floor(size * Math.random())];
}
randImg()
这段代码对我很有用,我按id获取每个表框,然后给它们一个随机图像作为列表。因此表中的所有4个图像都不同。谢谢!但我如何在文件中写入这段代码。我是否只需将这段代码放在HTML文件的我的分区中?我是否需要更改表中的任何内容?要运行它,只需在并将此代码放入其中。您不需要更改表。好的,谢谢,现在我在我的ide中收到消息“unresolved function or method map()”,这是因为您的导航器不支持新版本的JavaScript。
var picsList = [
'picto/rain%20(1).png',
'picto/rainsun.png',
'picto/cloudy.png',
'picto/sun.png'
];
function randImg() {
const size = picsList.length;
document.getElementById('i1').src = picsList[Math.floor(size * Math.random())];
document.getElementById('i2').src = picsList[Math.floor(size * Math.random())];
document.getElementById('i3').src = picsList[Math.floor(size * Math.random())];
document.getElementById('i4').src = picsList[Math.floor(size * Math.random())];
}
randImg()