조회 : 116
안녕하세요?
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에 내용을 띄울려고 하는데 글로벌 변수로 선언해도 가장 마지막 값을 참조해 버려서 글로벌 변수는 답이 아닌거 같습니다.
제목 |
---|
태그 적용 |
여보세요? 손님도 적으실 수 있습니다. 11 |
일 |
월 |
화 |
수 |
목 |
금 |
토 |
---|---|---|---|---|---|---|
6.1
|
6.2
|
6.3
|
6.4
|
6.5
|
6.6
|
6.7
|
6.8
|
6.9
|
6.10
|
6.11
|
6.12
|
6.13
|
6.14
|
6.15
|
6.16
|
6.17
|
6.18
|
6.19
|
6.20
|
6.21
|
6.22
|
6.23
|
6.24
|
6.25
|
6.26
|
6.27
|
6.28
|
6.29
|
6.30
|
7.1
|
7.2
|
7.3
|
7.4
|
7.5
|