gpt4 book ai didi

javascript - 计算器生成控制台日志,但屏幕上没有显示任何内容

转载 作者:行者123 更新时间:2023-12-02 14:01:56 25 4
gpt4 key购买 nike

我必须使用 JavaScript/jquery 构建这个计算器,但是当我按下按钮时,屏幕上不会显示任何数字,它会显示在控制台日志中,没有错误,我只需要帮助弄清楚如何获取数字显示在屏幕上任何帮助表示赞赏

function init(){

console.log('start');
var num1 = 0;
var num2 = 0;
var op = '';
var ON = '';

$('.digit, .operator orange').click(function(){
var ID = this.id;
console.log(ID);
calculator(this);


});

function calculator(obj){
console.log(ON);
var ID = obj.id;
var type = obj.classList[1];


if(type === 'digit'){
if (ON == true){
$('#calculation').empty();
}

$('#calculation').append(ID);
console.log(ID + '!')
}else if(type === 'operator orange'){
op =obj.textContent;
if(num1 == 0){
num1 = $('#calculation').text();


}else{
num2 = $('#calculation').text();
num1 = eval(num1 + op + num2);
}

$('#calculation').empty();
}else if (type ==='operator orange'){
num2 = $('#calculation').text();
num1 = eval(num1 + op + num2);
$('#calculation').empty();
$('#calculation').append(num1);
ON = true;
num1= 0;
num2= 0;


}else if(type === 'clear'){
$('#calculation').empty();
num1=0;
num2=0;
ON = false;


}


}

}
$(document).ready(init);
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<title>Calculator</title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<div id="calculator">
<div id="calculation">0</div>
<div class="buttons">
<ul>
<li id="clear" class="clear darkgray">C</li>
<li class="empty darkgray">&nbsp;</li>
<li class="empty darkgray">&nbsp;</li>
<li id="/" class="operator orange">/</li>
</ul>
<ul>
<li id="7" class="digit">7</li>
<li id="8" class="digit">8</li>
<li id="9" class="digit">9</li>
<li id="*" class="operator orange">*</li>
</ul>
<ul>
<li id="4" class="digit">4</li>
<li id="5" class="digit">5</li>
<li id="6" class="digit">6</li>
<li id="-" class="operator orange">-</li>
</ul>
<ul>
<li id="1" class="digit">1</li>
<li id="2" class="digit">2</li>
<li id="3" class="digit">3</li>
<li id="+" class="operator orange">+</li>
</ul>
<ul>
<li id="0" class="digit wide nobottom">0</li>
<li id="." class="digit nobottom">.</li>
<li id= "=" class="equals orange nobottom">=</li>
</ul>
</div>

</div>
<script src="js.js"></script>
</body>
</html>

最佳答案

问题就在这里

$('#calculation').append(num1);

将 .append 更改为 .text

关于javascript - 计算器生成控制台日志,但屏幕上没有显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40344887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com