简体   繁体   English

如何防止小数点重复?

[英]How do I prevent the decimal point from repeating?

I am in the early process of creating a calculator and just testing out my code.我正处于创建计算器的早期过程,只是在测试我的代码。 How do I prevent the decimal point from repeating?如何防止小数点重复? Basically, I need the decimal point to only be displayed once when clicked.基本上,我需要小数点在点击时只显示一次。

 let display = document.querySelector('.display'); let numberButtons = document.querySelectorAll('.number').forEach(function(numberButtons) { numberButtons.addEventListener('click', showNumber) function showNumber() { if (display.textContent == 0) { display.innerHTML = ""; } display.innerHTML += numberButtons.innerHTML; } });
 <body> <div class='container'> <div class="display">0</div> <button class='number' id='one'>1</button> <button class='number' id="two">2</button> <button class='number' id='decimal-point'>.</button> </div> </body>

Another option is to disable button after it has been pressed.另一种选择是在按下按钮后禁用按钮。 Make sure if you clear values to reset back to (disable: false).确保您清除了要重置回的值(禁用:false)。

function singleClick() { document.getElementById("decimal-point").disabled = true; function singleClick() { document.getElementById("decimal-point").disabled = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用javascript / jquery防止在HTML文本字段中键入小数点? - How can I use javascript/jquery to prevent a decimal point from being typed into an HTML text field? 我正在尝试使用jquery和javascript将用户输入限制为有效价格。 如何防止第二个小数点 - I'm trying to limt user input to a valid price using jquery & javascript. How do i prevent second decimal point 如何防止一个 function 重复,直到另一个函数的 if 条件满足并运行? - How do I prevent one function from repeating until another function's if condition is met and it runs? 如何在Javascript中获取浮点数的小数位? - How do I get the decimal places of a floating point number in Javascript? 如何使用 Plotly 格式化数字并将小数点增加到左侧? - How do I format a number and increase the decimal point to the left with Plotly? 如何防止CKEditor将十六进制字符转换为十进制字符? - How do i Prevent CKEditor convert hexadecimal character to decimal character? 如何防止数组连续重复元素? - How to prevent an array from repeating elements consecutively? 解析JSON时如何防止删除小数点? - How to prevent removing decimal point when parsing JSON? 如何限制正则表达式中的小数点? - How can I limit the decimal point in a regex? 如何计算从0开始的小数点偏移 - How to calculate the decimal point offset from 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM