let box = document.querySelector('div')
box.innerText ='ccccc'// 不解析标签
box.innerHTML ='<strong>dddd</strong>'// 可解析标签
案例
随机点名
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Document</title><style>div{display: inline-block;width: 150px;height: 30px;border: 1px solid pink;align-items: center;text-align: center;line-height: 30px;}</style></head><body>
抽中的:<div>1</div><script>let box = document.querySelector('div')functiongetRandomIntInclusive(min, max){
min = Math.ceil(min);
max = Math.floor(max);return Math.floor(Math.random()*(max - min +1))+ min;//含最大值,含最小值}let arr =['刻晴','香菱','凝光','甘雨','钟离']let random =getRandomIntInclusive(0,arr.length -1)
box.innerHTML = arr[random]
arr.splice(random,1)</script></body></html>
随机背景
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Document</title><style>body{background-image:url(../../resources/image/background/1.jpg);background-size:1920px 1080px ;}</style></head><body><div></div><script>functiongetRandomIntInclusive(min, max){
min = Math.ceil(min);
max = Math.floor(max);return Math.floor(Math.random()*(max - min +1))+ min;//含最大值,含最小值}let num =getRandomIntInclusive(1,9)
document.body.style.backgroundImage =`url(../../resources/image/background/${num}.jpg)`</script></body></html>