Computer >> 컴퓨터 >  >> 프로그램 작성 >> HTML

HTML DOM 저장소 key() 메서드

<시간/>

HTML DOM Storage key() 메서드는 스토리지 객체의 지정된 인덱스에서 키 이름을 반환하는 데 사용됩니다. 인덱스는 key() 메소드에 매개변수로 전달됩니다. 스토리지 개체는 세션 개체 또는 localStorage 개체일 수 있습니다.

구문

다음은 −

의 구문입니다.

localStorage를 사용하는 저장소 key() 메서드 -

localStorage.key(index);

sessionStorage를 사용한 Storage key() 메소드 -

sessionStorage.key(index);

여기서 index는 이름을 얻으려는 키 번호를 나타내는 정수 유형입니다.

예시

Storage key() 메서드의 예를 살펴보겠습니다.

<!DOCTYPE html>
<html>
<body>
<h1 style="text-align:center">storage key() method example</h1>
<p>Get the first object key name by clicking on the below button</p>
<button onclick="keyName()">GET NAME</button>
<p id="Sample"></p>
<script>
   function keyName() {
      var k = localStorage.key(1);
      document.getElementById("Sample").innerHTML ="The key name of the second
      localStorage object is "+k;
   }
</script>
</body>
</html>

출력

이것은 다음과 같은 출력을 생성합니다 -

HTML DOM 저장소 key() 메서드

GET NAME 버튼 클릭 시 -

HTML DOM 저장소 key() 메서드