递增和递减

时间:2016-02-18 07:04:38

标签: javascript html

我想为增量和减量值设置2个按钮。因此,如果我增加var k = 4的值并按下减量按钮,它会减小4到3的值

我试过这个

var k = 1;

function qtrFunction(k, v) {
  document.getElementById('qtrrr').innerHTML = k;
  k++;
  document.getElementById('qtrrr').innerHTML = v;
  v--;
}
v = k;
console.log(k);
<div style="color:white; font-size:70px;">QTR <span id="qtrrr" style="color:green; border-radius:10px; padding:5px 20px 5px 20px;">0</span>
</div>


<button onclick="qtrFunction(k)" style="padding:15px">Increase Quart</button>
<button onclick="qtrFunction(v)" style="padding:15px">Decrease Quart</button>

7 个答案:

答案 0 :(得分:5)

  • 避免使用innerHTML,因为它触发了设置值字符串的解析,这是一项昂贵的操作(尽管它优于document.write)。请考虑设置.textContent
  • 您尚未声明v本地,因此您将收到有关未定义符号的错误。
  • 您不需要两个值。

像这样:

<script type="text/ecmascript">
    var value = 0; // initialize a global variable

    function updateAndDisplay(increment) {
        if( increment ) value++;
        else            value--;
        document.getElementById('qtrrr').textContent = value;
    }
</script>

<button onclick="updateAndDisplay(true)" style="padding:15px">Increase Quart</button>
<button onclick="updateAndDisplay(false)" style="padding:15px">Decrease Quart</button>

答案 1 :(得分:2)

<强>的Javascript

<script>
    var k = 1;
    function qtrFunction(V) {
        if(V=="I")
        {
          k++;
        }
        else
        {
          k--;
        }
        document.getElementById('qtrrr').innerHTML = k;
        console.log(k); 
    }       

</script>

<强> HTML

<div style="color:white; font-size:70px;">QTR  <span id="qtrrr" style="color:green; border-radius:10px; padding:5px 20px 5px 20px;">0</span></div>


<button onclick="qtrFunction('I')" style="padding:15px">Increase Quart</button>
<button onclick="qtrFunction('D')" style="padding:15px">Decrease Quart</button>

答案 2 :(得分:0)

试试这个。它可能会对你有帮助。

var cnt = 0;
function qtrFunction(val) {
  if(val=='k'){
    cnt = document.getElementById('qtrrr').innerHTML = (cnt+1);	        
  }else {
    cnt = document.getElementById('qtrrr').innerHTML = (cnt-1);
  }
}
<div style="color:white; font-size:70px;">QTR  <span id="qtrrr" style="color:green; border-radius:10px; padding:5px 20px 5px 20px;">0</span></div>
		
		
<button onclick="qtrFunction('k')" style="padding:15px">Increase Quart</button>
<button onclick="qtrFunction('v')" style="padding:15px">Decrease Quart</button>

答案 3 :(得分:0)

var k = 1;
    function qtrFunction(condition) {
        document.getElementById('qtrrr').innerHTML= condition?k++:k--;
    } 

<button onclick="qtrFunction(true)" style="padding:15px">Increase Quart</button>
<button onclick="qtrFunction(false)" style="padding:15px">Decrease Quart</button>

答案 4 :(得分:0)

您可以执行以下操作:

在调用js函数时,只需传递动作(incr或decr),然后获取当前值,并根据按下的按钮incr或decr值。之后就回来了。

function qtrFunction(action) {
  var value = document.getElementById("qtrrr").innerHTML;
  if (action == "incr") {
    value++;
  } else {
    value--;
  }
  document.getElementById("qtrrr").innerHTML = value;
}
<div style="color:white; font-size:70px;">QTR <span id="qtrrr" style="color:green; border-radius:10px; padding:5px 20px 5px 20px;">0</span>
</div>


<button onclick="qtrFunction('incr')" style="padding:15px">Increase Quart</button>
<button onclick="qtrFunction('decr')" style="padding:15px">Decrease Quart</button>

答案 5 :(得分:0)

This Code May be Helpful for you

<!DOCTYPE html>
<html>
<head>
   <title></title>
   <script type="text/javascript">
        var value = 0; // initialize a global variable

        function incrementAndDisplay() {
        value++;
       alert(value);
        document.getElementById('qtrrr').textContent = value;

        }
       function decrementAndDisplay() {
        value--;
       alert(value);
        document.getElementById('qtrrr').textContent = value;

        }

        console.log(value);


   </script>
</head>
<body>
<button onclick="incrementAndDisplay()" style="padding:15px">Increase Quart</button>
<button onclick="decrementAndDisplay()" style="padding:15px">Decrease Quart</button>
</body>
</html>

答案 6 :(得分:0)

这应该会有所帮助:

&#13;
&#13;
var sampleVar = 0;

document.getElementById('qtrrr').innerHTML = sampleVar;

function stepping(step) {
  sampleVar += step;
  document.getElementById('qtrrr').innerHTML = sampleVar;
}
&#13;
<div style="color:white; font-size:70px;">
  QTR <span id="qtrrr" style="color:green; border-radius:10px; padding:5px 20px 5px 20px;"></span>
</div>

<button onclick="stepping(1)" style="padding:15px">Increase Quart</button>
<button onclick="stepping(-1)" style="padding:15px">Decrease Quart</button>
&#13;
&#13;
&#13;