在Javascript中增加和减少值
我相信我的问题可能有一个简单的解决方案,但我不知道如何解决它在Javascript中增加和减少值,javascript,ecmascript-6,Javascript,Ecmascript 6,我相信我的问题可能有一个简单的解决方案,但我不知道如何解决它 设x; 让我们一起行动吧; 设horamin=10; 设horamax=15; 函数hora(x,op){ 如果(x>horamin&&x{ x=parseInt(document.querySelector(“.hora”).innerText); op=“soma”; 贺拉(x,op); //document.querySelector(“.hora”).innerText=x+1; }); document.querySele
设x;
让我们一起行动吧;
设horamin=10;
设horamax=15;
函数hora(x,op){
如果(x>horamin&&x{
x=parseInt(document.querySelector(“.hora”).innerText);
op=“soma”;
贺拉(x,op);
//document.querySelector(“.hora”).innerText=x+1;
});
document.querySelector(“.horaD”).addEventListener('click',(事件)=>{
x=parseInt(document.querySelector(“.hora”).innerText);
op=“diminui”;
贺拉(x,作品)
});
一切都按计划进行,直到我达到
horamin
或horamax
值。然后它就会停止工作,不管我点击了多少次。我怎样才能修好它 如果数字超出范围,您只需阻止递增和递减,我猜您希望始终允许以下操作之一:
if (op == 'soma') {
if(x >= horamax) return;
document.querySelector(".hora").innerText = x +1
} else {
if(x <= horamin) return;
document.querySelector(".hora").innerText = x -1
}
if(op==“soma”){
如果(x>=horamax)返回;
document.querySelector(“.hora”).innerText=x+1
}否则{
如果(x)控制台中有什么错误?当x
超出horamin
和horamax
的界限时,您预计会发生什么?您的hora()
函数设计为在超出这些界限时不执行任何操作。
if (op == 'soma') {
if(x >= horamax) return;
document.querySelector(".hora").innerText = x +1
} else {
if(x <= horamin) return;
document.querySelector(".hora").innerText = x -1
}