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

[attribute] HTML - onfocus 속성 ★ - 포커스 설정 시 실행 (= onfocus속성 = 온포커스속성) ※ onfocus이벤트 = 온포커스이벤트

목차
  1. onfocus 예제 - 포커스 '설정/해제' 시 배경색 변경
  2. onfocus 정의
  3. onfocus 구문

 

onfocus 예제 - 포커스 '설정/해제' 시 배경색 변경

 

이름: <input type="text" id="name" onfocus="focusBg(this.id)" onblur="blurBg(this.id)"><br>

별명: <input type="text" id="nick" onfocus="focusBg(this.id)" onblur="blurBg(this.id)">


<script>

function focusBg(x) {

  document.getElementById(x).style.background = "yellow";

}

function blurBg(x) {

  document.getElementById(x).style.background = "silver";

}

</script>

 

결과보기

 

onfocus 정의

 

요소에 포커스(= 해당 요소를 마우스커서로 찍는 행위) 시, 실행할 스크립트 내용 지정.

 


 

1. onfocus 속성 사용 가능 태그 종류

모든 보이는 HTML 요소

  • 예외: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, <title>

  

2. 

  • 주로, <input>, <select>, <a> 요소에서 사용.
  • onblur 속성 (= 포커스 해제 시 실행할 스크립트 지정)과 주로 함께 사용.

3. 

모든 브라우저 지원.

 

 

onfocus 구문

 

HTML 방식

  • <element onfocus="myScript">

 

JS 방식 (3가지)

  • object.onfocus = ()=>{myScript};
  • object.onfocus = function(){myScript};
  • object.addEventListener("focus", myScript);



[속성값]

 

myScript

포커스 설정 시, 실행할 JS 코드

  • 주의: 함수명 사용 시, 다른 곳은 전부 소괄호까지 사용해 함수 호출하나 핑크색 myScript 자리는 "함수명만" 사용해 함수 호출.
  • 사용 방식 통일 권장.

 


방문 감사합니다. (즐겨찾기 등록: Ctrl + D)

분류 제목
게시물이 없습니다.
20/18
목록
찾아주셔서 감사합니다. Since 2012