2016-09-12 2 views
-1

첫 번째 섹션이 작동합니다. +를 누르면 작동합니다. + 뒤에 아무 것도 작동하지 않습니다. 플러스를 누르면 두 번째 버튼 세트가 나타나지만 아무 것도 누르지 않습니다. 그건 그렇고, 나는 계산기를 만들고있다. 어떤첫 번째 버튼이 작동하고 두 번째 버튼이 작동하지 않습니다

<html> 
<head> 
<title> 
JavaScript 
</title> 
</head> 
<body> 
<script> 
var first = "" 
document.write('<button onclick="one()">1</button>'); 
document.write('<button onclick="two()">2</button><br/>'); 
document.write('<button onclick="three()">3</button>'); 
document.write('<button onclick="four()">4</button><br/>'); 
document.write('<button onclick="five()">5</button>'); 
document.write('<button onclick="six()">6</button><br/>'); 
document.write('<button onclick="seven()">7</button>'); 
document.write('<button onclick="eight()">8</button><br/>'); 
document.write('<button onclick="nine()">9</button>'); 
document.write('<button onclick="zero()">0</button><br/>'); 
document.write('<button onclick="add()">+</button><br/>'); 
function one(){ 
first = first + "1"; 
} 
function two(){ 
first = first + "2"; 
} 
function three(){ 
first = first + "3"; 
} 
function four(){ 
first = first + "4"; 
} 
function five(){ 
first = first + "5"; 
} 
function six(){ 
first = first + "6"; 
} 
function seven(){ 
first = first + "7"; 
} 
function eight(){ 
first = first + "8"; 
} 
function nine(){ 
first = first + "9"; 
} 
function zero(){ 
first = first + "0"; 
} 
function add(){ 
document.body.innerHTML = ''; 
var second = "" 
document.write('<button onclick="one()">1</button>'); 
document.write('<button onclick="two()">2</button><br/>'); 
document.write('<button onclick="three()">3</button>'); 
document.write('<button onclick="four()">4</button><br/>'); 
document.write('<button onclick="five()">5</button>'); 
document.write('<button onclick="six()">6</button><br/>'); 
document.write('<button onclick="seven()">7</button>'); 
document.write('<button onclick="eight()">8</button><br/>'); 
document.write('<button onclick="nine()">9</button>'); 
document.write('<button onclick="zero()">0</button><br/>'); 
document.write('<button onclick="equal()">=</button><br/>'); 
function one(){ 
second = second + "1"; 
} 
function two(){ 
second = second + "2"; 
} 
function three(){ 
second = second + "3"; 
} 
function four(){ 
second = second + "4"; 
} 
function five(){ 
second = second + "5"; 
} 
function six(){ 
second = second + "6"; 
} 
function seven(){ 
second = second + "7"; 
} 
function eight(){ 
second = second + "8"; 
} 
function nine(){ 
second = second + "9"; 
} 
function zero(){ 
second = second + "0"; 
} 
function equal(){ 
first = Math.floor; 
second = Math.floor; 
answer = first + second; 
document.write(answer); 
} 
} 
</script> 
</body> 
</html> 
+1

1990 년대에는 자바 스크립트가 필요했습니다. 'document.write()'를 사용하지 말고, DOM 수정 함수를 사용하는 법을 배워라. – Barmar

답변

0

당신이 원래의 솔루션을 가지고이 문제는 두 번째 계산기의 기능 추가() 함수의 개인 범위에 있다고했지만, HTML <button>의 글로벌 범위에있는, 그래서 거기 방법을 사용하여 '두 번째'변수에 숫자를 추가하는 함수에 액세스 할 수 있습니다.

다음은이 작품 이봐 참조

<html> 
 
<head> 
 
<title> 
 
JavaScript 
 
</title> 
 
</head> 
 
<body> 
 
<script> 
 

 
first = "" 
 
document.write('<button onclick="first_one()">1</button>'); 
 
document.write('<button onclick="first_two()">2</button>'); 
 
document.write('<button onclick="first_three()">3</button>'); 
 
document.write('<button onclick="first_four()">4</button>'); 
 
document.write('<button onclick="first_five()">5</button>'); 
 
document.write('<button onclick="first_six()">6</button>'); 
 
document.write('<button onclick="first_seven()">7</button>'); 
 
document.write('<button onclick="first_eight()">8</button>'); 
 
document.write('<button onclick="first_nine()">9</button>'); 
 
document.write('<button onclick="first_zero()">0</button>'); 
 
document.write('<button onclick="first_add()">+</button>'); 
 

 
function first_one(){ 
 
first = first + "1"; 
 
} 
 
function first_two(){ 
 
first = first + "2"; 
 
} 
 
function first_three(){ 
 
first = first + "3"; 
 
} 
 
function first_four(){ 
 
first = first + "4"; 
 
} 
 
function first_five(){ 
 
first = first + "5"; 
 
} 
 
function first_six(){ 
 
first = first + "6"; 
 
} 
 
function first_seven(){ 
 
first = first + "7"; 
 
} 
 
function first_eight(){ 
 
first = first + "8"; 
 
} 
 
function first_nine(){ 
 
first = first + "9"; 
 
} 
 
function first_zero(){ 
 
first = first + "0"; 
 
} 
 

 
function first_add() { 
 

 
second = "" 
 

 
document.write('<button onclick="second_one()">1</button>'); 
 
document.write('<button onclick="second_two()">2</button><br/>'); 
 
document.write('<button onclick="second_three()">3</button>'); 
 
document.write('<button onclick="second_four()">4</button><br/>'); 
 
document.write('<button onclick="second_five()">5</button>'); 
 
document.write('<button onclick="second_six()">6</button><br/>'); 
 
document.write('<button onclick="second_seven()">7</button>'); 
 
document.write('<button onclick="second_eight()">8</button><br/>'); 
 
document.write('<button onclick="second_nine()">9</button>'); 
 
document.write('<button onclick="second_zero()">0</button><br/>'); 
 
document.write('<button onclick="second_equal()">=</button><br/>'); 
 

 
} 
 

 
function second_one(){ 
 
second = second + "1"; 
 
} 
 
function second_two(){ 
 
second = second + "2"; 
 
} 
 
function second_three(){ 
 
second = second + "3"; 
 
} 
 
function second_four(){ 
 
second = second + "4"; 
 
} 
 
function second_five(){ 
 
second = second + "5"; 
 
} 
 
function second_six(){ 
 
second = second + "6"; 
 
} 
 
function second_seven(){ 
 
second = second + "7"; 
 
} 
 
function second_eight(){ 
 
second = second + "8"; 
 
} 
 
function second_nine(){ 
 
second = second + "9"; 
 
} 
 
function second_zero(){ 
 
second = second + "0"; 
 
} 
 

 
function second_equal(){ 
 
document.write((parseInt(first) + parseInt(second)).toString()); 
 
} 
 
</script> 
 
</body> 
 
</html>

+0

위대한 작품, 함수 안에 함수를 선언했다. 지금은 의미가 있습니다. –

0

전역에서 기능의 두 세트, 코드입니다.

<script> 
var data =""; 
var first = ""; 
var sum = ""; 
document.write('<button onclick="one()">1</button>'); 
document.write('<button onclick="two()">2</button><br/>'); 
document.write('<button onclick="three()">3</button>'); 
document.write('<button onclick="four()">4</button><br/>'); 
document.write('<button onclick="five()">5</button>'); 
document.write('<button onclick="six()">6</button><br/>'); 
document.write('<button onclick="seven()">7</button>'); 
document.write('<button onclick="eight()">8</button><br/>'); 
document.write('<button onclick="nine()">9</button>'); 
document.write('<button onclick="zero()">0</button><br/>'); 
document.write('<button onclick="add()">+</button>'); 
document.write('<button onclick="equal()">=</button><br/>'); 
document.write('<div id =\"sum\"> </div><br/>'); 
function one(){ 
data = data + "1"; 
} 
function two(){ 
data = data + "2"; 
} 
function three(){ 
data = data + "3"; 
} 
function four(){ 
data = data + "4"; 
} 
function five(){ 
data = data + "5"; 
} 
function six(){ 
data = data + "6"; 
} 
function seven(){ 
data = data + "7"; 
} 
function eight(){ 
data = data + "8"; 
} 
function nine(){ 
data = data + "9"; 
} 
function zero(){ 
data = data + "0"; 
} 
function add(){ 
first = data; 
data= ""; 
} 

function equal(){ 
sum = parseInt(first) + parseInt(data); 



//document.getElementById("sum").remove(); 
document.getElementById("sum").innerHTML = sum ; 

     data =""; 
    first = ""; 
sum = ""; 
} 




</script> 
0

코드 예에는 몇 가지 문제가 있습니다. 당신이 + 클릭 한 후 숫자를 클릭하면 마크 업에 배선되어

  • 방법 창에 따라서 정의 될 필요가있는 모든 방법은 추가 기능을 외부에서 정의 된 함수를 가리

  • 또한 add() 메서드를 재정의하는 방법은 add 메서드 범위 내에서만 사용할 수 있으며 전역 적으로 정의 된 메서드를 재정의하지는 않지만 add 메서드 (document.write가 아닌) , 그들은 add 함수 내부의 메소드를 가리킬 것이다.

  • Math.floor는 메서드이며 인수가 필요합니다. Math.Floor

관련 문제