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

[attribute] HTML - onblur 속성 ★ - 포커스 해제 이벤트 시 실행 (= onblur속성 = 온블러속성) ※ onblur이벤트 = 온블러이벤트

목차
  1. onblur 예제 - 포커스 해제 시 입력값 유효성 검사
  2. onblur 정의
  3. onblur 구문
  4. onblur 예제 - 포커스 '설정/해제' 시 배경색 변경

 

onblur 예제 - 포커스 해제 시 입력값 유효성 검사

 

영문이름: <input type="text" name="name" id="name" onblur="homzzang()">


<script>

function homzzang() {

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

  x.value = x.value.toUpperCase();

}

</script>

 

결과보기

 

onblur 정의

 

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

 


 

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

모든 보이는 HTML 요소

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

 

2.

  • 사용자 입력값 유효성 검사할 때 주로 사용.
  • onfocus 속성 (= 포커스 설정 시 실행할 스크립트 지정)과 함께 사용해 포커스 설정 전후 효과 지정 가능.

 

3.

모든 브라우저 지원.

 


onblur 구문

 

HTML 방식

  • <element onblur="myScript">

 

JS 방식 (3가지)

  • object.onblur = ()=>{myScript};
  • object.onblur = function(){myScript};
  • object.addEventListener("blur", myScript);



[속성값]

 

myScript

포커스 해제 시, 실행할 JS 코드

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

 


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

[단일 요소 경우]


이름: <input type="text" id="hz" onfocus="focusBg()" onblur="blurBg()">


<script>

function focusBg() {

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

}

function blurBg() {

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

}

</script>

 

결과보기


[여러 요소 경우]

 

이름: <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>

 

결과보기 



분류 제목
form HTML - <input type="image"> 태그 - 이미지로 된 submit 버튼 (= 인풋이미지타입…
attribute HTML - oncut 속성 - 요소 내용 잘라내기 시 실행 (= oncut속성 = 온컷속성) ※ oncut…
attribute HTML - style 속성 ★ - 인라인 스타일 지정 (= style속성 = 스타일속성)
attribute HTML - onpause 속성 - (오디오/비디오) 일시정지 시 실행 (= onpause속성 = 온포스속성…
attribute HTML - optimum 속성 - 최적 게이지값 지정 (= optimum속성 = 옵티멈속성) ※ 미터태그 …
attribute HTML - onkeydown 속성 - 키보드키 내려갈 때 실행 (= onkeydown속성 = 온키다운속성)…
attribute HTML - onoffline 속성 - 브라우저가 오프라인으로 작동할 때 실행 (= onoffline속성 =…
form HTML - <input type="password"> 태그 - 패스워드 입력 (= 비밀번호 입력 = 인풋패…
attribute HTML - ondrop 속성 - 드래그 한 요소를 드롭 할 때 실행 (= ondrag속성 = 온드래그속성)…
attribute HTML - start 속성 - 순서있는 리스트 시작값 설정 (= start속성 = 스타트속성) ※ 오엘태그…
attribute HTML - onstorage 속성 - 로컬스토리지 데이터 변경 시 실행 (= onstorage속성 = 온스…
form HTML - <input type="month"> 태그 - 연월(년월) 입력 (= 인풋먼스타입/먼스타입인풋/…
attribute HTML - high 속성 - <meter>에서 높다 생각되는 값 지정 (= high속성 = 하이속성) ※ …
form HTML - <input type="checkbox"> 태그 - 체크박스 모양 INPUT (= 인풋체크박스타…
attribute HTML - onplaying 속성 - (오디오/비디오) 재생 중 실행 (= onplaying속성 = 온플레…
form HTML - <input type="button"> 태그 - 버튼 모양 인풋 (= 인풋버튼타입/버튼타입인풋/…
1/18
목록
찾아주셔서 감사합니다. Since 2012