第一个按钮工作,第二个按钮不工作

时间:2016-09-12 23:15:03

标签: javascript html button onclick

第一部分有效。当我按+时,它有效。 +后没有任何作用。我按下加号,第二组按钮出现,但按下它们什么都不做。顺便说一句,我正在制作一个计算器。

<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>

3 个答案:

答案 0 :(得分:0)

您对原始解决方案的问题是第二个计算器中的函数位于add()函数的私有范围内,但HTML <button>位于全局范围内,因此没有他们可以访问这些功能,为你的第二个&#39;添加数字。变量

这是您的代码,在全局范围内具有两组功能

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

嘿,看看这是否有效。

{{1}}

答案 2 :(得分:0)

您的代码示例有几个问题。

  • 需要在窗口上定义连接在标记上的方法,因此当您在单击+后单击任何数字时,所有方法都指向在add函数之外定义的函数

    < / LI>
  • 此外,你在add()中重新定义方法的方法只能在add的函数范围内使用,它们不会覆盖全局定义的方法,但是当你在add方法中访问它们时(不是在document.write)中,他们将指向add function中的方法。

  • Math.floor是一种方法,需要参数。Math.Floor