f

출력 결과가 이상하게 다르네요....

안녕하세요.
자바스크립트 초보라 이렇게 질문 드립니다.
 
동일한 내용을 출력하는데 함수에서 호출 할 경우와 그렇지 않을 경우의 결과가 다릅니다.
왜 그런가요?
 
 
[예제1]
<html>
<head>
<script type="text/javascript">
function print_form()
{
 document.write(document.forms.length);
 document.write(document.forms[0].elements[1].name);
}
</script>
</head>
<body>
<form>
name:<input type="text" name="name" /><br />
phone:<input type="text" name="phone" /><br />
</form>
<form>
name2:<input type="text" name="name2" /><br />
phone2:<input type="text" name="phone2" /><br />
</form>
<input type="submit" onclick="print_form()" />

</body>
</html>
 
결과 : 2, name 이 정상 출력 되어야 하는데 이상하게 2만 출력이 되네요.
 
[예제2]
<html>
<head>
</head>
<body>
<form>
name:<input type="text" name="name" /><br />
phone:<input type="text" name="phone" /><br />
</form>
<form>
name2:<input type="text" name="name2" /><br />
phone2:<input type="text" name="phone2" /><br />
</form>
<script type="text/javascript">
document.write(document.forms.length);
document.write(document.forms[0].elements[0].name);
</script>

</body>
</html>
 
결과 : 2, name 이 정상 출력 됩니다.
|

댓글 3개

document.write(document.forms.length+document.forms[0].elements[1].name); 요렇게 처리해야 할듯 싶내요...
새로운 페이지가 생성되면서 잃으면서 다음건 출력 안되는거내요.
도통 모르겠네요
일단은 두 페이지가 소스가 다르네요

document.forms[0].elements[0].name
document.forms[0].elements[1].name

http://math88.com.ne.kr/computer/js/JSmanual/7-2-6-2.html
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
14년 전 조회 1,234
14년 전 조회 1,605
14년 전 조회 1,371
14년 전 조회 1,591
14년 전 조회 1,896
14년 전 조회 1,440
14년 전 조회 1,734
14년 전 조회 1,346
14년 전 조회 1,362
14년 전 조회 1,878
14년 전 조회 3,754
14년 전 조회 1,300
14년 전 조회 2,740
14년 전 조회 1,498
14년 전 조회 1,407
14년 전 조회 2,300
14년 전 조회 2,220
14년 전 조회 1,189
14년 전 조회 2,123
14년 전 조회 2,564
14년 전 조회 2,041
14년 전 조회 1,195
14년 전 조회 2,597
14년 전 조회 1,410
14년 전 조회 1,197
14년 전 조회 1,121
14년 전 조회 1,896
14년 전 조회 2,251
14년 전 조회 1,753
14년 전 조회 1,198
14년 전 조회 1,422
14년 전 조회 1,896
14년 전 조회 1,946
14년 전 조회 1,186
14년 전 조회 3,970
14년 전 조회 1,862
14년 전 조회 5,095
14년 전 조회 1,413
14년 전 조회 2,396
14년 전 조회 1,456
14년 전 조회 1,379
14년 전 조회 2,279
14년 전 조회 1,269
14년 전 조회 2,240
14년 전 조회 1,335
14년 전 조회 2,107
14년 전 조회 1,632
14년 전 조회 4,672
14년 전 조회 2,666
14년 전 조회 1,520
14년 전 조회 1,342
14년 전 조회 1,925
14년 전 조회 1,548
14년 전 조회 1,670
14년 전 조회 2,739
14년 전 조회 1,375
14년 전 조회 3,479
14년 전 조회 3,463
14년 전 조회 1,745
14년 전 조회 1,432
14년 전 조회 2,928
14년 전 조회 1,539
14년 전 조회 1,807
14년 전 조회 1,289
14년 전 조회 2,408
14년 전 조회 1,747
14년 전 조회 1,818
14년 전 조회 2,092
14년 전 조회 1,243
14년 전 조회 4,302
14년 전 조회 1,549
14년 전 조회 2,398
14년 전 조회 1,909
14년 전 조회 1,303
14년 전 조회 5,294
14년 전 조회 1,148
14년 전 조회 3,254
14년 전 조회 1,618
14년 전 조회 1,282
14년 전 조회 1,678
14년 전 조회 8,426
14년 전 조회 1,589
14년 전 조회 2,644
14년 전 조회 2,111
14년 전 조회 1,684
14년 전 조회 4,732
14년 전 조회 1,350
14년 전 조회 1,806
14년 전 조회 1,419
14년 전 조회 1,477
14년 전 조회 2,178
14년 전 조회 2,982
14년 전 조회 1,536
14년 전 조회 1,649
14년 전 조회 1,562
14년 전 조회 1,922
14년 전 조회 1,173
14년 전 조회 1,663
14년 전 조회 1,161
14년 전 조회 1,597