메뉴 건너뛰기

2021.01.24 00:07

9장 실습문제 10번

조회 수 1204 댓글 2

<!DOCTYPE html>
<html>
<head>
    <title>계산기</title>
    <style type="text/css">
        #butn {width: 70px; padding: 10px}
        #txt {width: 280px; height: 20px;}
    </style>
    <script type="text/javascript">
        var first = 0;

        function back()
        {
            var len = txt.value.length;
            var backsp = txt.value.substring(0,len-1);

            txt.value = backsp;

        }
        function del()
        {
            txt.value="0";
            first=0;
        }
        function enter()
        {
            var exp = txt.value;
            txt.value=eval(exp);
        }
        function inp(obj)
        {    
            if(first==0) //초기 0 지우기
            {
                txt.value=""
            }
            first++;
            var clik = obj.value;
            var exp = txt.value;
            txt.value+= clik;
        }
    </script>
</head>
<body>
    <h3>계산기 만들기</h3>
    <hr>
    <form>
        <input id="txt" type="text" value="0">
    <br>
    <table>
        <tbody>
        <tr>
            <td><input id="butn" type="button" value="BACK" onclick="back()"></td>
            <td><input id="butn" type="button" value="CE" onclick="del()"></td>
            <td><input id="butn" type="button" value="C" onclick="del()"></td>
            <td><input id="butn" type="button" value="=" onclick="enter()"></td>
        </tr>
        <tr>
            <td><input id="butn" type="button" value="7" onclick="inp(this)"></td>
            <td><input id="butn" type="button" value="8" onclick="inp(this)"></td>
            <td><input id="butn" type="button" value="9" onclick="inp(this)"></td>
            <td><input id="butn" type="button" value="/" onclick="inp(this)"></td>
        </tr>
        <tr>
            <td><input id="butn" type="button" value="4" onclick="inp(this)"></td>
            <td><input id="butn" type="button" value="5" onclick="inp(this)"></td>
            <td><input id="butn" type="button" value="6" onclick="inp(this)"></td>
            <td><input id="butn" type="button" value="*" onclick="inp(this)"></td>
        </tr>
        <tr>
            <td><input id="butn" type="button" value="1" onclick="inp(this)"></td>
            <td><input id="butn" type="button" value="2" onclick="inp(this)"></td>
            <td><input id="butn" type="button" value="3" onclick="inp(this)"></td>
            <td><input id="butn" type="button" value="-" onclick="inp(this)"></td>
        </tr>
        <tr>
            <td><input id="butn" type="button" value="0" onclick="inp(this)"></td>
            <td><input id="butn" type="button" value="+" onclick="inp(this)"></td>
            <td><input id="butn" type="button" value="NONE"></td>
            <td><input id="butn" type="button" value="NONE"></td>
        </tr>
        </tbody>
    </table>
    </form>
</body>
</html>

9장 실습문제 10번에 대한 코드를 저렇게 짜봤는데 script 부분에 따로 var txt= document.getElementById("txt")를 하지않아도 작동하네요.. 오히려 저 getElement ... 를 넣으면 작동을 하지 않아요. 왜인가요...??

?
  • ?
    관리자 2021.01.24 10:13 Files첨부 (1)

    질문이 2가지네요. 한 가지 씩 알아봅시다.
    1. 먼저 var txt= document.getElementById("txt")를 사용하지 않아도 작동하는 이유?

    HTML4까지는 그냥 var txt= document.getElementById("txt") 코드를 사용하지 않고
    txt.value = "0"과 같이 그냥 txt를 사용하면 HTML 브라우저들이 id가 txt인 HTML엘리먼트로 처리하였습니다.
    하지만 HTML5에서는 이 방식은 표준이 아닙니다. 
    그럼에도 불구하고 HTML5 이전 시절에 작성된 웹페이지들을 위해 현재의 브라우저들이 
    txt.value = "0"과 같이 그냥 txt만 사용해도 처리해주고 있는 것 같습니다.
    앞으로 var txt= document.getElementById("txt")를 이용하는 것이 바람직합니다.

     

    2. var txt= document.getElementById("txt")를 넣으면 작동하지 않는다?
    현재의 코드에서 아래와 같은 위치에 넣으면 오류가 발생합니다.
    <script type="text/javascript">
            var first = 0;
            var txt= document.getElementById("txt"); // 이곳
    왜냐하면 이 코드가 실행되는 시점에서는 아직 <body> 이하 부분이 처리(파싱)되지 않아
    브라우저는 어떤 태그가 있는지 알 수 없기 때문입니다.
    그래서 이 문제를 해결하는 방법은 여러가지 가 있는데 다음과 같이 하면 됩니다.

    <script type="text/javascript">
            var first = 0;

    뒤에, HTML 페이지가 완전히 로딩된 후, txt= document.getElementById("txt"); 가 실행되도록

    window.onload를 달면 됩니다.

    몇가지 이유로, 첨부에 이미지로 넣었으니 참고하세요.

  • ?
    왜죠 ㅠㅠ 2021.01.24 23:28
    아하 알겠습니다!! 답변 감사합니다~!

QnA

공부하면서 궁금했던 것을 질문해보세요.

List of Articles
번호 제목 글쓴이 날짜 조회 수
공지 읽어주세요! 연습문제 및 실습문제 정답 공개에 관한 공지입니다. file 관리자 2017.06.20 18275
403 <li> 1 랄라 2019.05.29 69
402 <script>태그의 위치 1 열심히 공부하는 독자 2020.08.15 58
401 07장 10번 답좀 알고싶습니다... 2 abcd 2017.06.11 1088
400 10장 window.open 질문입니다. 1 멍청이 2019.08.23 379
399 10장 실습문제 6번 전역변수 설정에 관한 질문 1 제발요 2019.12.04 632
398 10장 실습문제5번 질문입니다, 1 asdfdef 2019.08.30 1158
397 10장 연습문제 10번 질문입니다. 2 웹린 2022.11.03 159
396 10장 예제 10-8에 오류가 있습니다. 1 file 삼오칠 2021.05.22 289
395 10장 오픈챌린지 5 은성 2017.04.07 2247
394 10장 오픈챌린지 testr 2017.11.19 888
393 10장 오픈챌린지 질문입니다. 1 질문있어요 2019.11.10 485
392 10장 이론문제 10번 질문입니다. 3 하늘하 2019.05.15 849
391 11장 6번 1 mj 2018.11.29 67539
390 11장 6번 으엉 2019.11.27 187591
389 11장 실습문제 4번 1 웹프 2020.06.17 456
388 11장 실습문제 6번 arc()문제 제발요 2019.12.06 1093
387 11장 실습문제 6번 질문입니다. 1 file 메론 2018.11.29 884
386 11장 실습문제 7번 응용 ㅇㅇ 2020.06.16 154
385 12장 연습문제 4번 HTML 요청 1 file 윤세아 2020.06.10 641
384 13-5 하고 13-6 error 1 WebWeb 2020.12.09 148
목록
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 21 Next
/ 21
위로