js const options = apple banana orange kiwi grape; var i = -1;function randomSelect const randomIndex = MathfloorMathrandom optionslength; ifi!==-1 ifi == randomIndex alert调用函数+i; random
在代码中,p1被定义为一个元素,但是并没有给它赋值,所以点击p1时调用randomSelect()函数返回的值没有被输出到任何地方,导致输出undefined。如果想让函数的返回值输出到页面上,需要将返回值赋给p1元素的innerHTML属性或者textContent属性。例如:
p1.onclick=function(){
p1.innerHTML = randomSelect();
}
原文地址: https://www.cveoy.top/t/topic/cgRo 著作权归作者所有。请勿转载和采集!