Computer >> 컴퓨터 >  >> 프로그래밍 >> HTML

HTML DOM Link type 속성 – 연결된 문서의 MIME 유형 설정 및 반환 방법


HTML DOM Link type 속성이란?

HTML DOM의 type 속성<link> 요소로 연결된 외부 문서의 유형(MIME 타입)을 설정하거나 반환하는 기능입니다. 예를 들어 스타일시트는 "text/css", 자바스크립트 파일은 "text/javascript"처럼 문서의 종류를 브라우저에 알려주는 역할을 합니다.

문법(Syntax)

Link type 속성의 기본 문법은 다음과 같습니다.

  • type 속성값 반환하기
linkObject.type
  • type 속성에 유효한 값 설정하기
linkObject.type = value

참고: 유효한 값으로는 "text/javascript", "text/css", "image/gif" 등이 있습니다.

예제(Example)

다음은 Link type 속성의 실제 사용 예제입니다.

<!DOCTYPE html>
<html>
<head>
<title>Link type</title>
<link id="extStyle" rel="stylesheet" href="style.css" type="myStyle">
</head>
<body>
<form>
<fieldset>
<legend>Link-type</legend>
<input type="button" value="Correct Type" onclick="correctType()">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
    var divDisplay = document.getElementById("divDisplay");
    var extStyle = document.getElementById("extStyle");
    divDisplay.textContent = 'The linked document type: '+extStyle.type+' is not compatible';
    function correctType(){
       extStyle.type = 'text/css';
       divDisplay.textContent = 'Congrats! The linked document type: '+extStyle.type+' is compatible';
    }
</script>
</body>
</html>

위 예제에서 연결된 'style.css' 파일의 내용은 다음과 같습니다.

form {
    width:70%;
    margin: 0 auto;
    text-align: center;
}
* {
    padding: 2px;
    margin:5px;
}
input[type="button"] {
    border-radius: 10px;
}

실행 결과(Output)

위 코드를 실행하면 다음과 같은 결과가 출력됩니다.

'Correct Type' 버튼을 클릭하기 전 −

HTML DOM Link type 속성 – 연결된 문서의 MIME 유형 설정 및 반환 방법

'Correct Type' 버튼을 클릭한 후 −

HTML DOM Link type 속성 – 연결된 문서의 MIME 유형 설정 및 반환 방법