>  Q&A  >  본문

색상 변경 방법: 버튼 사용

<p>이 함수에 더 적은 수의 코드를 작성할 수 있는 방법이 있나요? 버튼 중 하나를 누를 때 두 가지 색상 사이를 전환하고 싶습니다. green() 함수는 red와 정반대입니다. </p> <pre class="lang-html Prettyprint-override"><code><h1 id="header">빨간색 또는 녹색</h1> <button id="redButton" onclick="red()">빨간색 <button id="greenButton" onclick="green()">녹색 <pre class="brush:php;toolbar:false;">function red() { document.getElementById("header").innerHTML = "빨간색" document.getElementById('header').style.color = "빨간색" document.getElementById('redButton').style.color = "흰색" document.getElementById('redButton').style.BackgroundColor = "빨간색" document.getElementById('greenButton').style.color = "검은색" document.getElementById('greenButton').style.BackgroundColor = "회색" }</pre></p>
P粉111927962P粉111927962410일 전425

모든 응답(1)나는 대답할 것이다

  • P粉254077747

    P粉2540777472023-09-06 11:09:33

    으아악 으아악 으아악

    회신하다
    0
  • 취소회신하다