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

JavaScript의 문서에서 링크의 href 속성을 찾는 방법은 무엇입니까?

<시간/>

자바스크립트 document.links.href 를 제공했습니다. href 를 얻으려면 필수 링크의 속성입니다. 이 방법은 배열의 특정 요소를 표시하는 방법과 동일하게 작동합니다. 간단히 논의해 보겠습니다.

예시-1

다음 예에서는 세 개의 링크가 제공되었습니다. document.links.href 사용 세 번째 링크는 출력에서 ​​실행됩니다. 배열과 동일하게 따릅니다. 첫 번째 링크는 0번째 인덱스를, 두 번째 링크는 1번째 인덱스를 가져오는 식입니다.

<html>
<body>
   <a href="https://www.tutorialspoint.com/javascript/index.htm">Javascript</a><br>
   <a href="https://www.tutorialspoint.com/java8/index.htm">Java</a><br>
   <a href="https://www.tutorialspoint.com/spring/index.htm">Spring</a>
<p id="links"></p>
<script>
   document.getElementById("links").innerHTML =
   "The href attribute is: " + document.links[2].href;
</script>
</body>
</html>

출력

Javascript
Java
Spring
The href attribute is: https://www.tutorialspoint.com/spring/index.htm


예시-2

다음 예에서는 두 개의 링크가 제공되었습니다. document.links.href 사용 첫 번째 링크는 출력에서 ​​실행됩니다.

<html>
<body>
   <a href="https://www.tutorialspoint.com/javascript/index.htm">Javascript</a><br>
   <a href="https://www.tutorialspoint.com/java8/index.htm">Java</a>;
<p id="links"></p>
<script>
   document.getElementById("links").innerHTML =
   "The href attribute is: " + document.links[0].href;
</script>
</body>
</html>

출력

Javascript
Java
The href attribute is: https://www.tutorialspoint.com/javascript/index.htm