JavaScript는 CSS 스타일을 얻습니다.
구문:
nodeObject.style.cssProperty
그 중 nodeObject는 노드 객체이고 cssProperty는 CSS 속성입니다.
예:
document.getElementById("demo").style.height;
document.getElementById("demo").style.border;참고: "-"로 구분된 CSS 속성의 경우 "-"를 제거하고 "-" 뒤의 첫 번째 문자를 대문자로 표시합니다. 예:
background-color는 backgroundColor로 작성해야 합니다.
line-height는 lineHeight로 작성해야 합니다.
예:
document.getElementById("demo").style. backgroundColor;
document.getElementById("demo").style.lineHeight;예를 들어, 스타일을 가져옵니다. id="demo"인 노드:
<div id="demo" style="height:50px; width:250px; margin-top:10px; text-align:center; line-height:50px; background-color:#ccc;">
点击这里获取CSS样式
</div>
<script type="text/javascript">
document.getElementById("demo").onclick=function(){
alert(
"高度:"+this.style.height+"\n"+
"宽度:"+this.style.width+"\n"+
"上边距:"+this.style.marginTop+"\n"+
"对齐:"+this.style.textAlign+"\n"+
"行高:"+this.style.lineHeight+"\n"+
"背景颜色:"+this.style.backgroundColor
);
}
</script>위 코드를 약간 수정하여 HTML에서 CSS 스타일을 분리합니다.
<style>
#demo{
height:50px;
width:250px;
margin-top:10px;
text-align:center;
line-height:50px;
background-color:#ccc;
}
</style>
<div id="demo">
点击这里获取CSS样式
</div>
<script type="text/javascript">
document.getElementById("demo").onclick=function(){
alert(
"高度:"+this.style.height+"\n"+
"宽度:"+this.style.width+"\n"+
"上边距:"+this.style.marginTop+"\n"+
"对齐:"+this.style.textAlign+"\n"+
"行高:"+this.style.lineHeight+"\n"+
"背景颜色:"+this.style.backgroundColor
);
}
</script>분리한 후에는 CSS 스타일을 얻을 수 없습니다. HTML 코드의 CSS 스타일. 이는
nodeObject.style.cssProperty
가 DOM 노드의 style 속성에 의해 정의된 스타일을 얻기 때문입니다. style 속성이 존재하지 않거나 style 속성이 해당 스타일을 정의하지 않습니다. 스타일로는 얻을 수 없습니다.
즉, JavaScript는 해당 스타일을 가져오기 위해 <style> 태그나 CSS 파일로 이동하지 않고 스타일 속성에 정의된 스타일만 가져올 수 있습니다.
새로운 파일
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<div id="demo" style="height:50px; width:250px; margin-top:10px; text-align:center; line-height:50px; background-color:#ccc;">
点击这里获取CSS样式
</div>
<script type="text/javascript">
document.getElementById("demo").onclick=function(){
alert(
"高度:"+this.style.height+"\n"+
"宽度:"+this.style.width+"\n"+
"上边距:"+this.style.marginTop+"\n"+
"对齐:"+this.style.textAlign+"\n"+
"行高:"+this.style.lineHeight+"\n"+
"背景颜色:"+this.style.backgroundColor
);
}
</script>
</head>
<body>
</body>
</html>
시사
Clear
- 코스 추천
- 코스웨어 다운로드
현재 코스웨어를 다운로드할 수 없습니다. 현재 직원들이 정리하고 있습니다. 앞으로도 본 강좌에 많은 관심 부탁드립니다~
이 강좌를 시청한 학생들도 학습하고 있습니다.
PHP로 사업을 시작하는 방법에 대해 간단히 이야기해 보겠습니다.
웹 프론트 엔드 개발에 대한 빠른 소개
민망한 물건 백과사전 사이트를 모방한 Mini 버전 MVC 프레임워크의 대규모 실용 Tianlongbabu 개발
PHP 실용 개발 시작하기: 빠른 PHP 생성 [중소기업 포럼]
로그인 인증 및 클래식 게시판
컴퓨터 네트워크 지식 수집
빠른 시작 Node.JS 정식 버전
당신을 가장 잘 이해하는 프론트엔드 강좌: HTML5/CSS3/ES6/NPM/Vue/...[원본]
자신만의 PHP MVC 프레임워크 작성(깊이 있는 40개 장/자세한 내용/초보자가 발전하려면 읽어야 함)
















