使用 Javascript 更改背景颜色和内部文本
Change bg color and inner text with Javascript
我正在尝试制作一个简单的 bg 随机化器项目,该项目还使用随机颜色的 rgb 代码更新 h1 文本。
我似乎无法找到为什么这不起作用的解决方案:
const button= document.querySelector('button');
button.addEventListener('click',function(){
const h1 = document.querySelector('h1');
let r= math.floor((Math.random()+254)+1);
let g= math.floor((Math.random()+254)+1);
let b= math.floor((Math.random()+254)+1);
body.style.backgroundColor(rgb(r,g,b));
h1.innerText(`rgb(${r},${g},${b})`);
})
下面的代码改变了颜色。
似乎是多个问题
const h1 = document.querySelector('h1');
let r= math.floor((Math.random()+254)+1);
let g= math.floor((Math.random()+254)+1);
let b= math.floor((Math.random()+254)+1);
body.style.backgroundColor(rgb(r,g,b));
h1.innerText(`rgb(${r},${g},${b})`);
- 正文未定义
backgroundColor
不是函数
rgb
未定义
innerText
不是函数
const button= document.querySelector('button');
button.addEventListener('click',function(){
const h1 = document.querySelector('h1');
let r= Math.floor((Math.random()*254)+1);
let g= Math.floor((Math.random()*254)+1);
let b= Math.floor((Math.random()*254)+1);
document.body.style.backgroundColor = `rgb(${r},${g},${b})`;
h1.innerText = `rgb(${r},${g},${b})`;
})
<body>
<h1>Test Color</h1>
<button>Change Color</button>
</body>
body.style.backgroundColor
是一个 属性 而不是函数。
尝试 body.style.backgroundColor = "rgb(${r},${g},${b})"
我正在尝试制作一个简单的 bg 随机化器项目,该项目还使用随机颜色的 rgb 代码更新 h1 文本。
我似乎无法找到为什么这不起作用的解决方案:
const button= document.querySelector('button');
button.addEventListener('click',function(){
const h1 = document.querySelector('h1');
let r= math.floor((Math.random()+254)+1);
let g= math.floor((Math.random()+254)+1);
let b= math.floor((Math.random()+254)+1);
body.style.backgroundColor(rgb(r,g,b));
h1.innerText(`rgb(${r},${g},${b})`);
})
下面的代码改变了颜色。
似乎是多个问题
const h1 = document.querySelector('h1');
let r= math.floor((Math.random()+254)+1);
let g= math.floor((Math.random()+254)+1);
let b= math.floor((Math.random()+254)+1);
body.style.backgroundColor(rgb(r,g,b));
h1.innerText(`rgb(${r},${g},${b})`);
- 正文未定义
backgroundColor
不是函数rgb
未定义innerText
不是函数
const button= document.querySelector('button');
button.addEventListener('click',function(){
const h1 = document.querySelector('h1');
let r= Math.floor((Math.random()*254)+1);
let g= Math.floor((Math.random()*254)+1);
let b= Math.floor((Math.random()*254)+1);
document.body.style.backgroundColor = `rgb(${r},${g},${b})`;
h1.innerText = `rgb(${r},${g},${b})`;
})
<body>
<h1>Test Color</h1>
<button>Change Color</button>
</body>
body.style.backgroundColor
是一个 属性 而不是函数。
尝试 body.style.backgroundColor = "rgb(${r},${g},${b})"