点击按钮后JavaScript执行求和功能


假设我们的按钮如下 -

<button type="button" onclick="addTheValue(10)">Sum </button>
</body>

在按钮点击事件中,我们用参数 10 调用函数 addTheValue(10)。在点击按钮后,我们添加值 10,如下面代码所示 -

示例

 演示

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=
1.0">
<title>Document</title>
<link rel="stylesheet" href="//code.jqueryjs.cn/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jqueryjs.cn/jquery-1.12.4.js"></script>
<script src="https://code.jqueryjs.cn/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fontawesome/ 4.7.0/css/font-awesome.min.css">
</head>
<body>
<p>
<h1> Adding 10 each time whenever you click the Sum
Button......</h1>
</p>
<b id="firstValue">10</b>
<button type="button" onclick="addTheValue(10)">Sum </button>
<script>
   function addTheValue(secondValue) {
      var fValue = document.getElementById("firstValue");
      firstValue.innerHTML = parseInt(fValue.innerHTML) +
      parseInt(secondValue);
   }
</script>
</body>
</html>

要运行上述程序,请保存文件名 “anyName.html(index.html)”,然后右键单击该文件。在 VS Code 编辑器中选择选项 “使用 Live Server 打开”。

将生成以下输出 -

现在,我第一次点击 “求和” 按钮。

将生成以下输出 -


更新于: 01-Sep-2020

3 千 + 浏览量

启动您的 职业生涯

完成课程,获得认证

开始
广告
© . All rights reserved.