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

[API_Storage] JS - key(n) 메서드 - Storage 객체의 n번째 키 이름 반환. (= key메서드 = 키메서드)

목차
  1. key() 예제 - localStorage 객체의 첫 번째 키 이름 반환
  2. key() 정의
  3. key() 구문
  4. key() 예제 - sessionStorage 객체의 첫 번째 키 이름 반환
  5. key() 예제 - localStorage 객체의 모든 키 이름 반환

 

key() 예제 - localStorage 객체의 첫 번째 키 이름 반환

 

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

 

<p id="demo"></p>

 

<script>

function homzzang() {

  var x = localStorage.key(0);

  document.getElementById("demo").innerHTML = x;

}

</script> 

 

결과보기 

 

key() 정의

 

지정 색인번호의 key(키) 이름 반환.

 


 

1.

Storage 객체에 속하며, localStorage 또는 sessionStorrage 객체일 수 있음.

 

2.

IE8 이상 주요 최신 브라우저 모두 지원.

 

3.

Web Storage API의 일종

 

 

key() 구문

 

localStorage.key(index)

또는,

sessionStorage.key(index)

 


[매개변수]

 

index

필수. 이름을 얻기 원하는 키의 색인번호.

 


[반환값]

 

지정 key의 이름을 나타내는 문자열 반환.

 

 

key() 예제 - sessionStorage 객체의 첫 번째 키 이름 반환

 

<button onclick="createItems()">아이템 생성하기</button>

<button onclick="homzzang()">아이템 이름 얻기</button>

 

<p id="demo"></p>

 

<script>

function createItems() {

  sessionStorage.site = "홈짱닷컴";

  sessionStorage.host = "Homzzang.com";

  sessionStorage.open = 2012;

}

function homzzang() {

  var x = sessionStorage.key(1);

  document.getElementById("demo").innerHTML = x;

}

</script> 

 

결과보기 

 

key() 예제 - localStorage 객체의 모든 키 이름 반환

 

<button onclick="createItems()">아이템 생성하기</button>

<button onclick="displayItems()">아이템 표시하기</button>

 

<p id="demo"></p>

 

<script>

function createItems() {

  localStorage.setItem("today", Date.now());

  localStorage.setItem("host", "Homzzang.com");

  localStorage.setItem("open", 2012);

}

function displayItems() {

  document.getElementById("demo").innerHTML = "";

  for (let i = 0; i < localStorage.length; i++) {

  x = localStorage.key(i);

  document.getElementById("demo").innerHTML += x + "<br>";

  }

}

</script> 

 

결과보기

 

PS. StorageEvent 관련: key 속성 참고 바람.

 


분류 제목
HTML_Objects JS - <iframe> 객체 ★ - 타웹페이지삽입 (= iframe태그 = 아이프레임태그)
HTML_Objects JS - <img> 객체 ★ - 사진/그림/이미지 (= img태그 = 이미지태그)
HTML_Objects JS - <ins> 객체 - 텍스트삽입표시 (= ins태그 = 인스태그 = 인서트태그)
HTML_Objects JS - <input type="button"> 객체 - button타입 input태그 (= 인풋버튼태그)
HTML_Objects JS - <input type="checkbox"> 객체 ★ - checkbox타입 input태그 (= 인풋…
HTML_Objects JS - <input type="color"> 객체 - color타입 input태그 (= 인풋컬러태그) ※ …
HTML_Objects JS - <input type="date"> 객체 - date타입 input태그 (= 인풋데이트태그) ※ 날…
HTML_Objects JS - <input type="datetime"> 객체 - datetime타입 input태그 (= 인풋데이…
HTML_Objects JS - <input type="datetime-local"> 객체 - datetime-local타입 inp…
HTML_Objects JS - <input type="email"> 객체 - email타입 input태그 (= 인풋이메일태그)
HTML_Objects JS - <input type="file"> 객체 ★ - file타입 input태그 (= 인풋파일태그) ※ …
HTML_Objects JS - <input type="hidden"> 객체 ★ - hidden타입 input태그 (= 인풋히든태그…
HTML_Objects JS - <input type="image"> 객체 ★ - image타입 input태그 (= 인풋이미지태그)…
HTML_Objects JS - <input type="month"> 객체 - month타입 input태그 (= 인풋먼스태그) ※ …
HTML_Objects JS - <input type="number"> 객체 - number타입 input태그 (= 인풋넘버태그) …
HTML_Objects JS - <input type="password"> 객체 - password타입 input태그 (= 인풋패스…
HTML_Objects JS - <input type="radio"> 객체 ★ - radio타입 input태그 (= 인풋라디오태그 …
HTML_Objects JS - <input type="range"> 객체 - range타입 input태그 (= 인풋레인지태그) ※…
HTML_Objects JS - <input type="reset"> 객체 - reset타입 input태그 (= 인풋리셋태그) ※ …
HTML_Objects JS - <input type="search"> 객체 - search타입 input태그 (= 인풋서치태그) …
57/67
목록
찾아주셔서 감사합니다. Since 2012