2016-09-12 75 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

20世紀90年代,他們希望自己的Javascript回來。不要使用'document.write()',學習如何使用DOM修改函數。 – Barmar

回答

0

你有沒有跟你原來的解決方案的問題是,在第二計算器的功能是在add()函數的私人範圍,但HTML <button> s爲在全球範圍內,所以沒有他們可以通過這種方式訪問​​函數以將數字添加到「第二個」變量中。

這裏是你的代碼,有兩套功能在全球範圍內

<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

相關問題