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
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
13년 전 조회 700
13년 전 조회 1,067
13년 전 조회 906
13년 전 조회 1,060
13년 전 조회 1,337
13년 전 조회 935
13년 전 조회 1,201
13년 전 조회 779
13년 전 조회 800
13년 전 조회 1,325
13년 전 조회 3,196
13년 전 조회 753
13년 전 조회 2,187
13년 전 조회 963
13년 전 조회 865
13년 전 조회 1,756
13년 전 조회 1,688
13년 전 조회 693
13년 전 조회 1,599
13년 전 조회 2,028
13년 전 조회 1,437
13년 전 조회 684
13년 전 조회 2,101
13년 전 조회 900
13년 전 조회 678
13년 전 조회 647
13년 전 조회 1,371
13년 전 조회 1,750
13년 전 조회 1,224
13년 전 조회 714
13년 전 조회 849
13년 전 조회 1,336
13년 전 조회 1,291
13년 전 조회 627
13년 전 조회 3,424
13년 전 조회 1,291
13년 전 조회 4,450
13년 전 조회 855
13년 전 조회 1,850
13년 전 조회 933
13년 전 조회 832
13년 전 조회 1,738
13년 전 조회 709
13년 전 조회 1,678
13년 전 조회 766
13년 전 조회 1,549
13년 전 조회 1,076
13년 전 조회 4,137
13년 전 조회 2,135
13년 전 조회 954
13년 전 조회 778
13년 전 조회 1,341
13년 전 조회 982
13년 전 조회 1,108
13년 전 조회 2,201
13년 전 조회 853
13년 전 조회 2,909
13년 전 조회 2,900
13년 전 조회 1,156
13년 전 조회 860
13년 전 조회 2,394
13년 전 조회 987
13년 전 조회 1,241
13년 전 조회 763
13년 전 조회 1,887
13년 전 조회 1,184
13년 전 조회 1,242
13년 전 조회 1,579
13년 전 조회 679
13년 전 조회 3,755
13년 전 조회 1,006
13년 전 조회 1,883
13년 전 조회 1,380
13년 전 조회 752
13년 전 조회 4,729
13년 전 조회 600
13년 전 조회 2,723
13년 전 조회 1,061
13년 전 조회 728
13년 전 조회 1,105
13년 전 조회 7,875
13년 전 조회 1,045
13년 전 조회 2,136
13년 전 조회 1,582
13년 전 조회 1,121
13년 전 조회 4,177
13년 전 조회 789
13년 전 조회 1,269
13년 전 조회 891
13년 전 조회 948
13년 전 조회 1,638
13년 전 조회 2,439
13년 전 조회 971
13년 전 조회 1,084
13년 전 조회 1,035
13년 전 조회 1,402
13년 전 조회 641
13년 전 조회 1,108
13년 전 조회 612
13년 전 조회 1,027
🐛 버그신고