HTML DOM 문서 링크
예시
문서의 링크 수:
let numb = document.links.length;
문서에서 첫 번째 링크의 URL을 가져옵니다.
let url = document.links[0].href;
문서에서 첫 번째 링크의 URL을 가져옵니다.
let url = document.links.item(0).href;
아래에 더 많은 예가 있습니다.
정의 및 사용
이 links
속성은 문서에 있는 모든 링크의 컬렉션을 반환합니다.
forms
속성은 HTMLCollection 을 반환합니다 .
속성 은 links
읽기 전용입니다.
컬렉션의 링크는 href 속성이 있는 <a> 및 <area> 요소를 나타냅니다.
HTML컬렉션
HTMLCollection 은 HTML 노드의 모음입니다 .
컬렉션의 노드는 인덱스 번호로 액세스할 수 있습니다. 인덱스는 0에서 시작합니다.
length 속성 은 컬렉션의 요소 수를 반환합니다.
통사론
document.links
속성
Property | Description |
length | The number of elements in the collection. |
행동 양식
Method | Description |
[index] | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
item(index) | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
namedItem(id) | Returns the element with the specified id. Returns null if the id does not exist. |
반환 값
유형 | 설명 |
물체 | HTMLCollection 개체 . 문서의 모든 <a> 및 <area> 요소. 요소는 문서에 나타나는 대로 정렬됩니다. |
더 많은 예
id="myLink"인 요소의 URL을 가져옵니다.
let url = document.links.namedItem("myLink").href;
문서의 첫 번째 링크에 빨간색 테두리를 추가합니다.
document.links[0].style.border = "5px solid red";
모든 링크를 반복하고 각각의 URL(href)을 출력합니다.
const links = document.links;
let text = "";
for (let i = 0; i < links.length; i++) {
text += links[i].href + "<br>";
}
브라우저 지원
document.links
DOM 레벨 1(1998) 기능입니다.
모든 브라우저에서 완벽하게 지원됩니다.
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |