공대여자님... 좋은 의견 부탁드립니다.

1

안녕하세요?

phpschool javascrip Q/A에 올렸는데 아무도 답을 주지 않아서 여기에 올려봅니다..
요상 스런 방법으로 값은 찾을 수 있지만 이게 답이 안거 같아서 정확한 답을 알고자 이렇게 글 올립니다.


질문을 어떻게 해야 할지 참 난감합니다.
일단 예제 소스를 적어 보겠습니다.

<html>
<title>테스트</title>
<head>
<script type="text/javascript" language="javascript">
var test = function (value)
{
    this.values = document.getElementById(value);
    this.getTest();
}

test.prototype.getTest = function ()
{
    var divs    = document.createElement("div");
    this.values.parentNode.insertBefore(divs, this.values);

    var buttons    = document.createElement("button");
        buttons.setAttribute('value', '테스트');
        buttons.style.cursor    = 'pointer';
        if (document.addEventListener)
        {
            buttons.addEventListener("click", this.getMode, false);
        }
        else if (document.attachEvent)
        {
            buttons.attachEvent("onclick", this.getMode);
        }
        divs.appendChild(buttons);
}

test.prototype.getMode = function ()
{
    alert(this.values.value);
}
</script>
</head>
<body>
<table width="100%">
<form name="frm">
<tr>
    <td>
        <textarea name="contents1" id="contents1" style="display: ">cccccc</textarea>
        <script type="text/javascript" language="javascript">
        try { new test('contents1'); } catch (e) { ; }
        </script>
    </td>
</tr>
<tr>
    <td>
        <textarea name="contents2" id="contents2" style="display: ">aaaaa</textarea>
        <script type="text/javascript" language="javascript">
        try { new test('contents2'); } catch (e) { ; }
        </script>
    </td>
</tr>
</form>
</table>
</body>
</html>

웨 예제를 실행해 보시면...
alert(this.values.value); 에서 에러가 발생합니다.
버튼을 클릭하면 textarea에 내용을 띄울려고 하는데 글로벌 변수로 선언해도 가장 마지막 값을 참조해 버려서 글로벌 변수는 답이 아닌거 같습니다.

댓글
  • No Nickname
    No Comment
  • 권한이 없습니다.
    {{m_row.m_nick}}
    -
목록형 📷 갤러리형
제목
태그 적용
여보세요? 손님도 적으실 수 있습니다. 11
7.27
7.28
7.29
7.30
7.31
8.1
8.2
8.3
8.4
8.5
8.6
8.7
8.8
8.9
8.10
8.11
8.12
8.13
8.14
8.15
8.16
8.17
8.18
8.19
8.20
8.21
8.22
8.23
8.24
8.25
8.26
8.27
8.28
8.29
8.30
8.31
9.1
9.2
9.3
9.4
9.5
9.6