본문 바로가기
개발공부 일지/자바스크립트

자바스크립트)window.getComputedStyle 와 getPropertyValue('')

by Box Cat 2021. 8. 5.
728x90
반응형

window.getComputedStyle: 해당 태그의 CSS 전부를 읽어옴

 

getPropertyValue(''): CSS속성 별로 접근 방법

 

 

<!DOCTYPE html>

<html>

<head>

    <meta charset='utf-8'>

    <meta http-equiv='X-UA-Compatible' content='IE=edge'>

    <title>Page Title</title>

    <meta name='viewport' content='width=device-width, initial-scale=1'>

    <style>

        #txt {

            width: 200px;

            height: 60px;

            color: red;

        }

    </style>

</head>

<body>

    <input type="text" id="txt" value="테스트 텍스트">

    <script>

        // let para = document.querySelector('p');

        // let compStyles = window.getComputedStyle(para);

        // para.textContent = 'My computed font-size is ' +

        //     compStyles.getPropertyValue('font-size') +

        //     ',\nand my computed line-height is ' +

        //     compStyles.getPropertyValue('line-height') + '.';

        //아이디 선택자로 태그 접근

        var txt = document.querySelector("#txt");

        console.log(txt);

        console.log(txt.value);

        //해당 태그의 CSS 전부 읽어옴

        var style = window.getComputedStyle(txt);

        //css 속성별로 접근 방법

        console.log(style, style.getPropertyValue('color'));

    </script>

</body>

</html>

728x90
반응형

댓글