• 회원가입
  • 로그인
  • 구글아이디로 로그인

[DOM_Style] JS - color 속성 - 글자색깔 (=글자색상=글자색깔=글자컬러 = 텍스트색깔 = 컬러속성 = color속성, 상속O)

목차

  1. color 예제 - 텍스트 색상 설정
  2. color 정의
  3. color 구문
  4. color 예제 - 텍스트 색상 반환
  5. color 예제 - 텍스트 색상 토글
  6. (내부스타일/외부스타일) 경우 글자색 랜덤 변경 후 색상 유지

 

color 예제 - 텍스트 색상 설정

 

<h2 id="a">홈짱닷컴 Homzzang.com</h2>

<p id="b">홈페이지 제작관리 강의/대행</p>

<p id="c">각종 코딩언어/서버관리 매뉴얼</p>

<div id="d">그누보드/영카트/빌더 매뉴얼</div>

<br>

<button type="button" onclick="homzzang()">클릭</button>


<script>

function homzzang() {

  document.getElementById("a").style.color = "red";

  document.getElementById("b").style.color = "blue";

  document.getElementById("c").style.color = "green";

  document.getElementById("d").style.color = "tomato";

}

</script>

 

결과보기

 

color 정의

 

요소의 텍스트 글자색 반환/설정.

 


 

1.

  • 기본값: 없음.
  • CSS 버전: CSS1
  • CSS 속성: color

 

2.

모든 브라우저 지원.

 

 

color 구문

[주의] - 인라인스타일 방식에서만 작동.

 

반환

object.style.color

 

설정

object.style.color = "color|initial|inherit"

 


[속성값]

 

color

색상코드. 자세히보기

 

initial

기본값 사용.

 

inherit

부모요소의 속성값 상속.

 


[반환값]

 

요소의 인라인스타일에 적힌 텍스트 색깔을 나타내는 문자열 반환.

 

 

color 예제 - 텍스트 색상 반환

 

<p id="hz" style="color:red;">홈짱닷컴 Homzzang.com</p>


<button type="button" onclick="homzzang()">클릭</button>


<script>

function homzzang() {

  alert(document.getElementById("hz").style.color);

}

</script>

 

결과보기

 

color 예제 - 텍스트 색상 토글

 

<h1 style="color:red;">홈짱닷컴 Homzzang.com</h1>

<button onclick="changeColor()" >색깔 변경</button>


<script>

function changeColor() {

    const h1 = document.querySelector('h1');

    if(h1.style.color == 'red') {

        h1.style.color = 'blue';

    } else if(h1.style.color == 'blue') {

        h1.style.color = 'red';

    }      

}

</script>

 

결과보기

 

(내부스타일/외부스타일) 경우 글자색 랜덤 변경 후 색상 유지

 

<style>

#hz {color: red;}

</style>


<p id="hz">홈짱닷컴 Homzzang.com</p>


<button id="btn">클릭</button>


<script>

// 로컬 스토리지 방식1

const textElement = document.getElementById('hz');

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


button.addEventListener('click', function() {

  const randomColor = '#' + Math.floor(Math.random()*16777215).toString(16);

  textElement.style.color = randomColor;

  localStorage.setItem('textColor', randomColor);

});


if (localStorage.getItem('textColor')) {

  const savedColor = localStorage.getItem('textColor');

  const computedStyle = getComputedStyle(textElement);

  const currentColor = computedStyle.getPropertyValue('color');


  if (currentColor !== savedColor) {

    textElement.style.color = savedColor;

  }

}



/*

// 로컬 스토리지 방식2

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

const hz = document.getElementById('hz');


button.addEventListener('click', function() {

  const randomColor = '#' + Math.floor(Math.random()*16777215).toString(16); // 색깔랜덤

  hz.style.color = randomColor;

  localStorage.setItem('textColor', randomColor);

});


if (localStorage.getItem('textColor')) {

  hz.style.color = localStorage.getItem('textColor');

}

*/

</script>


결과보기



분류 제목
CSS_Objects JS - setProperty() 메서드 - 새 CSS속성설정 / 기존CSS속성수정 (= setPropert…
DOM JS - NodeList 속성 - 노드리스트 (= 노드목록)
Basic JS - this 키워드 ★★★★★ - 호출 방식에 따라 지시 대상 다름. (= 디스 키워드)
Functions JS - apply() 메서드 - 함수호출방법3 (= 어플라이메서드)
Functions JS - Closure - 클로저 (전역변수/지역변수/카운터)
Math JS - Math.atanh() 메서드 - 쌍곡선 아크탄젠트 (IE12 이상)
Math JS - Math.acosh() 메서드 - 쌍곡선 아크코사인 (IE12 이상)
Math JS - Math.asinh() 메서드 - 쌍곡선 아크사인 반환 (= asinh 메서드)
Math JS - Math.cbrt() 메서드 - 세제곱근
Math JS - Math.tanh() 메서드 - 쌍곡선 탄젠트 (IE12 이상)
Math JS - Math.trunc() 메서드 - 숫자의 정수부분 추출 (= trunc메서드 = 트런크메서드) (I…
Math JS - Math.cosh() 메서드 - 쌍곡선 코사인
Math JS - Math.sinh() 메서드 - 쌍곡선 사인값 (IE12 이상)
DOM_Event JS - initMouseEvent() 메서드 - 폐기예고. 마우스이벤트초기화
js JS - getRangeAt() 메서드
DOM_Document JS - styleSheets 속성 - 스타일시트집합
js JS - rules 속성 - styleSheets 객체의 속성
DOM_Event JS - cancelBubble 속성 - 부모요소로 이벤트 확산 방지 (= 상위요소로 이벤트확산취소 = 캔슬…
DOM_Event JS - composedPath() 메서드 - 이벤트객체흐름파악 (=이벤트실행객체경로파악 = 컴포우즈드패스메…
js JS - 특정요소 함수실행제외 (= 배열문자열포함검사) ★★★★★
61/67
목록
찾아주셔서 감사합니다. Since 2012