페이지에서 링크 추출 (exam0912.html)| > script

본문 바로가기
사이트 내 전체검색

script

페이지에서 링크 추출 (exam0912.html)|

페이지 정보

작성자 서방님 댓글 0건 조회 186회 작성일 11-11-10 14:49

본문

<?xml version="1.0" encoding="EUC-KR" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR" />
<title> [페이지에서 링크 추출] </title>
</head>
<body>
<div id="main">
   <div id="paragraph" style="border: double;">
     <p>The <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/link.asp">links</a> collection off of the document 
     <a href="http://www.w3.org/TR/html4/struct/objects.html">object</a
      consists of all hypertext links in the page, accessible as an array, starting with the first link in the page and moving down and to the right. 
      However, you can also add an identifier for each hypertext link and access it in the array through this identifier.</p>
      <p>Each item in the collection is a
      <a href="http://www.devguru.com/Technologies/ecmascript/quickref/link.html">link</a>
      object, which has properties of its own. Among these are those similar to what we found with location: host, protocol, port, search,
      and hash, each of which returns that specific piece of the hypertext link. You can also access the complete link through the href property,
      and the associated linked object (text) through text. This can be handy if you're pulling links from a document in a web page,
      into a handy sidebar reference or other functionality such as this. 
     </p>
   </div>

   <h3>References</h3>
   <div id="linkObject" style="border: solid;">
     <script type="text/javascript">
      //<![CDATA[
           for (var i=0; i<document.links.length; i++) {
               var link = document.links[i];
               document.writeln(link.innerText + " : " + link.href + "<br />");
           }
           // 링크의 텍스트를 출력 후 하이퍼링크의 주소를 출력

           document.writeln("<h1>link object</h1>");
           for (var propname in link) {
               document.writeln("link." + propname +" : " + link[propname] + "<br />");
           }
      //]]>
     </script> 
   </div>
</div>
</body>
</html>


프로그램 설명

for (var i=0; i<document.links.length; i++) {
    var link = document.links[i];
    document.writeln(link.outerText + " : " + link.href + "<br />");
}
// 문서의 모든 링크 객체는 배열의 형태로 각 요소에 저장되어있기 때문에 반복문을 통해 그 개수를 알 수 있다.
// 이렇게 조건을 걸어두고(이 소스에서의 링크는 3개다) 각 요소에 저장되어 있는 링크 객체를 객체참조변수 'link'에 넣는다.
// 객체를 참조하는 'link' 변수에 속성을 넣어 링크의 클릭되는 부분(텍스트)를 출력하고 하이퍼링크 주소를 출력한다.

document.writeln("<h1>link object</h1>");
for (var propname in link) {
    document.writeln("link." + propname +" : " + link[propname] + "<br />");
}

// for~in 구문을 통하여 link 객체의 프로퍼티에 대한 모든 정보를 'propname'라는 배열의 각 요소에 담는다.
// 객체안의 프로퍼티에 대해 전부 접근할 때 까지 자동으로 반복하며 출력문을 통해 프로퍼티의 이름과 프로퍼티를 직접 실행시
// 나타나는 정보를 출력해준다.

댓글목록

등록된 댓글이 없습니다.

Total 846건 14 페이지
게시물 검색

회원로그인

접속자집계

오늘
118
어제
225
최대
1,347
전체
154,877
Latest Crypto Fear & Greed Index

그누보드5
Copyright © 서방님.kr All rights reserved.