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

[DOM_Event] JS - focusin 이벤트 (= onfocusin 속성) - 해당요소(또는 자식요소) 포커스 될 때 이벤트 발생. (= onfocusin이벤트 = 온포커스인 이벤트)

목차

  1. focusin() 예제 - 포커스 시, input 요소 배경색 변경
  2. focusin() 정의
  3. focusin() 구문
  4. focusin() 예제 - (onfocusin / onfocusout) 이벤트 함께 걸 때
  5. focusin() 예제 - (focus / blur) 이벤트를 addEventListener() 메서드로 걸 때
  6. focusin() 예제 - (focusin / focusout) 이벤트를 addEventListener() 메서드로 걸 때

 

focusin() 예제 - 포커스 시, input 요소 배경색 변경

 

<input type="text" onfocusin="homzzang(this)">


<script>

function homzzang(x) {

  x.style.background = "yellow";

}

</script>

 

결과보기

 

focusin() 정의

 

요소가 포커스 상태일 때 이벤트 발생.

 


 

1.

onfocus 이벤트와 유사하나 아래와 같은 차이점 존재.

  • onfocus 이벤트 : 이벤트 확산 X
  • onfocusin 이벤트 : 이벤트 확산 O

∴ 요소 또는 해당 자식이 포커스 얻는지 확인하려면 onfocusin 사용 필요.

 

2.

focusin 이벤트의 반대는 focusout 이벤트임.

 

3.

  • (HTML / JS)의 속성으로 사용할 경우: onfocusout 형태
  • JS addEventListener() 메서드의 매개변수 경우: focusout 형태

 

4.

  • 이벤트확산: O
  • 취소가능성: X
  • 이벤트타입: FocusEvent
  • 지원HTML: 모든 HTML 요소 (단, <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, <title> 요소는 제외)
  • DOM 버전: Level 2 Events

 

5.

주요 브라우저 모두 지원.

[주의] onfocusout JS속성 방식 경우 오작동 할 수도 있으니, HTML 속성 방식이나 addEventListener() JS메서드 방식 이용해야 함.

 

 

focusin() 구문

 

HTML 속성 방식

<element onfocusin="myScript">

 

JS 속성 방식 (주의: Chrome, Safari, Opera 15+ 등에서 오작동 가능)

object.onfocusin = function(){myScript};

 

JS 매서드 방식

object.addEventListener("focusin", myScript);

 

 

focusin() 예제 - (onfocusin / onfocusout) 이벤트 함께 걸 때

 

<input type="text" id="hz" onfocusin="focusCase()" onfocusout="blurCase()">


<script>

function focusCase() {

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

}


function blurCase() {

  document.getElementById("hz").style.background = "red";

}

</script>

 

결과보기

 

focusin() 예제 - (focus / blur) 이벤트를 addEventListener() 메서드로 걸 때

※ 주의: (focus/blur) 이벤트 경우 기본적으로 이벤트 확산 안 하므로, addEventListener() 매서드의 useCapture 매개변수를 true로 설정 필요.

 

<form id="hzform">

  <input type="text" id="hz">

</form>


<script>

var x = document.getElementById("hzform");

x.addEventListener("focus", focusCase, true);

x.addEventListener("blur", blurCase, true);


function focusCase() {

  document.getElementById("hz").style.backgroundColor = "yellow";  

}


function blurCase() {

  document.getElementById("hz").style.backgroundColor = "";  

}

</script>

 

결과보기

 

focusin() 예제 - (focusin / focusout) 이벤트를 addEventListener() 메서드로 걸 때

※ 주의: (focusin / focusout) 이벤트 경우 기본적으로 이벤트 확산하므로, addEventListener() 메서드의 useCapture 매개변수를 true로 설정할 필요 없음.

 

<form id="hzform">

  <input type="text" id="hz">

</form>


<script>

var x = document.getElementById("hzform");

x.addEventListener("focusin", focusCase);

x.addEventListener("focusout", blurCase);


function focusCase() {

  document.getElementById("hz").style.backgroundColor = "yellow";  

}


function blurCase() {

  document.getElementById("hz").style.backgroundColor = "";  

}

</script>

 

결과보기



분류 제목
Window_Navigator JS - navigator.userAgent 속성 - 사용자 브라우저 정보 반환 (= navigator.us…
Window_Navigator JS - javaEnabled() 메서드 -
Window_Navigator JS - taintEnabled() 메서드 - JS1.2 버전에서 폐기완료. 브라우저에 데이터오염 있는지 확…
Window_Screen JS - availHeight 속성 ★ - 화면높이 (※ 작업표시줄 제외)
Window_Screen JS - availWidth 속성 ★ - 화면너비 (※ 작업표시줄 제외)
Window_Screen JS - colorDepth 속성 - 이미지 표시 위한 색상표 비트 심도를 픽셀 단위로 반환
Window_Screen JS - height 속성 ★ - 객체 높이 ※ screen.height: 화면 높이 (※ 작업표시줄 포함)
Window_Screen JS - pixelDepth 속성 - 방문자 화면의 색상해상도 (= 픽셀 당 비트 수) 반환 (IE10 이상…
Window_Screen JS - screen.width 속성 ★ - 스크린너비 (= 화면너비 = screen.width속성 = 스크…
DOM_Style JS - alignContent 속성(C) - 플렉스항목 세로정렬 (= alignContent속성 = 얼라인…
DOM_Style JS - alignItems 속성(C) - 플렉스아이템의 세로정렬방법 지정 (예: 상단정렬/하단정렬/균등배분…
DOM_Style JS - alignSelf 속성(I) - (플렉스아이템 수직정렬. 아이템 자체에 사용. align-self속…
DOM_Style JS - animation 속성 ★ - 애니일체 (= 움직임효과 = 이동효과 = 애니일괄 = 애니효과 = 애…
DOM_Style JS - animationDelay 속성 - 애니지연시간 (= animationDelay속성 = 애니메이션딜…
DOM_Style JS - animationDirection 속성 - 애니방향 (= 움직임방향 = 애니메이션디렉션속성, IE1…
DOM_Style JS - animationDuration 속성 - 애니지속시간 (= 애니완료소요시간 = 움직임완료소요시간 =…
DOM_Style JS - animationFillMode 속성 - 애니 미작동 스타일 (= 애니작동안할때 스타일 = 애니메이…
DOM_Style JS - animationIterationCount 속성 -애니메이션 반복횟수 설정/반환
DOM_Style JS - animationName 속성 - 키프레임명 (= 키프레임이름 = 애니이름 = 애니명 = 애니메이션…
DOM_Style JS - animationTimingFunction 속성 - 애니속도변화 (= 애니속도변경 = 애니속도곡선 …
42/67
목록
찾아주셔서 감사합니다. Since 2012