当随机数达到特定值时,h1 或按钮应更改颜色。

huangapple go评论65阅读模式
英文:

When random number hits certain value, h1 or button should change color

问题

const button = document.getElementById('button');
const h1 = document.getElementById('h1');

button.addEventListener('click', function () {
    const r = Math.floor(Math.random() * 255);
    const g = Math.floor(Math.random() * 255);
    const b = Math.floor(Math.random() * 255);
    const newColor = `rgb(${r}, ${g}, ${b})`;
    document.body.style.backgroundColor = newColor;
    h1.innerText = newColor;
});

button.addEventListener('click', function () {
    if (parseFloat(r) >= 150 || parseFloat(g) >= 150 || parseFloat(b) >= 150) {
        document.button.style.backgroundColor = '#000';
        document.button.style.color = '#fff';
    } else if (parseFloat(r) >= 60 || parseFloat(g) >= 60 || parseFloat(b) >= 60) {
        document.h1.style.color = '#fff';
    }
});
英文:

I made a random color generator and while it generates the random colors and displays the numbers, when it gets very dark on the page I wanted the h1 to become white and the button to become black if it became very bright.

const button = document.getElementById('button');
const h1 = document.getElementById('h1');

button.addEventListener('click', function (){
    const r = Math.floor(Math.random() * 255);
    const g = Math.floor(Math.random() * 255);
    const b = Math.floor(Math.random() * 255);
    const newColor = `rgb(${r}, ${g}, ${b})`;
    document.body.style.backgroundColor = newColor;
    h1.innerText = newColor;
});

button.addEventListener('click', function (){
    if(parseFloat(r >= 150 || g >= 150 || b >= 150)) {
        document.button.style.backgroundColor = '#000';
        document.button.style.color = '#fff';
    } else if(parseFloat(r >= 60 || g >= 60 || b >= 60)) {
        document.h1.style.color = '#fff';
    }
});

I think there is an issue with my parseFloat but I am unsure. I think the issue is that the number becomes a string and isn't converted back properly?

Right now my code just does not respond to the second portion where I ask the 'if' statements.

答案1

得分: 1

需要在一个函数中完成所有操作,因为变量在第二个函数中不可见。

也不需要调用 parseFloat()。您已经在这些变量中有数字。

document.buttondocument.h1 应该改为 buttonh1

button.addEventListener('click', function() {
  const r = Math.floor(Math.random() * 255);
  const g = Math.floor(Math.random() * 255);
  const b = Math.floor(Math.random() * 255);

  const newColor = `rgb(${r}, ${g}, ${b})`;
  document.body.style.backgroundColor = newColor;
  h1.innerText = newColor;
  if (r >= 150 || g >= 150 || b >= 150) {
    button.style.backgroundColor = '#000';
    button.style.color = '#fff';
  } else if (r >= 60 || g >= 60 || b >= 60) {
    h1.style.color = '#fff';
  }
});
英文:

You need to do everything in one function, because the variables aren't visible in the second function.

There's also no need to call parseFloat(). You already have numbers in those variables.

document.button and document.h1 should just be button and h1.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

button.addEventListener(&#39;click&#39;, function() {
  const r = Math.floor(Math.random() * 255);
  const g = Math.floor(Math.random() * 255);
  const b = Math.floor(Math.random() * 255);

  const newColor = `rgb(${r}, ${g}, ${b})`;
  document.body.style.backgroundColor = newColor;
  h1.innerText = newColor;
  if (r &gt;= 150 || g &gt;= 150 || b &gt;= 150) {
    button.style.backgroundColor = &#39;#000&#39;;
    button.style.color = &#39;#fff&#39;;
  } else if (r &gt;= 60 || g &gt;= 60 || b &gt;= 60) {
    h1.style.color = &#39;#fff&#39;;
  }
});

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月22日 06:21:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定