2014-03-24 5 views
0

자바 스크립트를 가르치려고합니다. 그러나 다음 코드가 작동하지 않는 이유를 알 수 없습니다. 어떤 아이디어?표시 날짜가 자바 스크립트

<!DOCTYPE html> 
<html> 
    <body> 

    <p> Click the button to display the date</p> 

    <button onclick="showDate()">HIT ME</button> 

    <script> 
     function displayDate() 
     { 
      document.getElementById("test").innterHTML=displayDate(); 
     } 
    </script> 

    <p id="test"></p> 
</body> 

복사 및 잘못, 여기에 실제 코드 붙여 넣기 :

<!DOCTYPE html> 
<html> 
    <body> 

    <p> Click the button to display the date</p> 

    <button onclick="showDate()">HIT ME</button> 

    <script> 
     function showDate() 
     { 
      document.getElementById("test").innterHTML=Date(); 
     } 
    </script> 

    <p id="test"></p> 
</body> 

+2

에 오타가? 'onclick' 함수의 이름은 아래의'script' 태그에서 작성한 함수의 이름과 동일하지 않습니다. 이 코드에는 많은 문제점이 있습니다. 어떻게 당신은 자바 스크립트를 배우려고합니까? – MattDiamant

+0

방금 ​​[재귀 함수] (http://www.codecademy.com/courses/javascript-lesson-205/0/1)를 수행했습니다. 함수가 _itself_를 호출하여 무한 루프가됩니다. ->'displayDate()'는 실제로 _called_되지 않으므로 실행되지 않습니다. – mathielo

+0

onclick = "showDate()"=> 어디 함수입니까? showDate() {...} ?? – frenchie

답변

3
document.getElementById("test").innterHTML=Date(); 

은이어야한다

document.getElementById("test").innerHTML=Date(); 
+0

감사. 그게 효과가 있었어! –

0

우선 innerHTML이되어야하는 innterHTML을 사용했습니다.
둘째로 기능, 나쁜 습관을 반복하고 있습니다. 날짜에 대한 정보는 http://www.w3schools.com/jsref/jsref_obj_date.asp을 참조하십시오. 루프가 필요하면 setInterval(function() {/* Action*/},100 /* Delay*/)을 사용하십시오. http://www.w3schools.com/jsref/met_win_setinterval.asp을 참조하십시오.

<!DOCTYPE html> 
<html> 
    <body> 

    <p> Click the button to display the date</p> 

    <button onclick="showDate()">HIT ME</button> 

    <script> 

     function displayDate() 
     { 
      document.getElementById("test").innerHTML=new Date().getTime() // Display timestamp 
     } 
    </script> 

    <p id="test"></p> 
</body> 
1

코드에서 여러 버그가있어 (당신은 문제가있는 경우 일반적으로 W3 스쿨 참조), 나는 다음 작업 중 하나를 게시하면

<!DOCTYPE html> 
<html> 
<head> 

<script> 
    function displayDate() 
    { 
     document.getElementById("test").innerHTML = Date(); 
    } 
</script> 
</head> 
<body> 

<p> Click the button to display the date</p> 

<button onclick="displayDate()">HIT ME</button> 


<p id="test"></p> 
</body> 
</html> 
  1. 그들 각각을 설명 할 것이다

    먼저 displayDate() 함수를 재귀 적으로 호출합니다. 이 함수는 아무런 조건없이 스스로를 호출하고 결과적으로 스택 오버플로로 끝납니다 (우리 모두는 이렇게하지 않습니다).

  2. 또 다른 문제는 click 이벤트 핸들러의 버튼을 함수 인 "showData 전혀 존재하지 않는다. (물론 원래 코드를 업데이트했는데 어쨌든 개요를 표시합니다.)

  3. 본문의 모든 스크립트는 브라우저에서 볼 때로드되고 실행되므로 더 이상 연습하지 말고 헤드 섹션에 두는 것이 좋습니다. 별도의 파일

  4. 당신은 당신이 일을 기대하고 무엇 "innerHTML을"

관련 문제