HTML文件中的输入框,控制JS文件中的数字
Input box in an HTML file that controls a number in a JS file
我有一个JS文件的函数,看起来像这样:
function Animation(options) {
defaultOptions = {
'direction' : 'clockwise',
'propertyName' : null,
'propertyValue' : null,
'duration' : 15,
'repeat' : null,
'callbackFinished' : null,
'callbackBefore' : null,
'callbackAfter' : null,
}
...
…然后它继续到动画的其余部分,使用这些值。
假设我想在一个html文件中有一个直接改变其中一个值的输入框,我该怎么做呢?
我只需要它的工作,所以如果你把"24"输入框,并按下一个按钮,它会设置"持续时间"为24。然后,当您刷新.html文件并触发动画时,它的持续时间将为24。
这可能吗?如果没有,有没有别的方法可以实现我想要的?
当然可以!
在我的代码片段中,如果你填充输入,它将覆盖默认选项,但如果你让它为空,它将保持15作为默认值。
希望对你有帮助!
function Animation(options) {
defaultOptions = {
'direction' : 'clockwise',
'propertyName' : null,
'propertyValue' : null,
'duration' : options.duration || 15,
'repeat' : null,
'callbackFinished' : null,
'callbackBefore' : null,
'callbackAfter' : null,
};
alert('duration is : ' + defaultOptions.duration);
};
function callAnimation() {
var input = document.getElementById('animationDuration');
Animation({duration: input.value});
};
<input type="number" step="1" id="animationDuration">
<button onclick="callAnimation()">Confirm</button>
您可以考虑定义一个构造函数,以便可以使用不同的设置实例化它。例如:
<!DOCTYPE html>
</head>
<body>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script>
function Animation() {
this.defaultOptions = {
'direction' : 'clockwise',
'propertyName' : null,
'propertyValue' : null,
'duration' : 15,
'repeat' : null,
'callbackFinished' : null,
'callbackBefore' : null,
'callbackAfter' : null,
}
// alternative ways to define and access the properties
var thisAnim = this;
this.direction = 'clockwise';
this.propertyName = 'duration';
this.propertyValue = thisAnim.defaultOptions.duration;
}
function SetAnimDuration(value) {
// set option value
myAnim.defaultOptions.duration=value;
// debugger display
document.getElementById('msg').innerHTML = myAnim.defaultOptions.duration;
console.log("duration=", myAnim.propertyValue);
}
var myAnim = new Animation();
</script>
</head>
<body>
Click a button to set the duration:
<br>
<button id="setDuration" type="button" class="button" onclick="SetAnimDuration('24')">Set Duration to 24</button>
<br>
<button id="setDuration" type="button" class="button" onclick="SetAnimDuration('35')">Set Duration to 35</button>
<br> Duration: <span id='msg'> N/A </span>
</body>
</html>
相关文章:
- 从文本文件js中读取列表
- 当我用ctrl+p搜索时,为什么要在谷歌chrome上使用inspect来调试缺少显示文件js
- 获取用于外部JavaScript文件(.js)的代码提示
- 如何将Javascript文件(.js文件)放在web服务器中
- 如何将 JavaScript 文件(.js 文件)添加到 C# 项目并从 C# 代码调用 JavaScript 方法
- 在 Node 中包含 javascript 文件.js无需 require('.') ing
- 如何使用jquery显示多个文件.多文件.js
- 使用 PDF 打开 PDF 文件.js而不将文件作为查询字符串传递
- 转换颜色十六进制值以用于三个 JSON 文件.js
- 如何使用 jQuery 选择多个文件.多文件.js插件?使用单个关闭按钮一次选择多个文件
- 如何在 Gulp 任务中间添加 CSS 文件.js
- 如何从Dropzone上传和删除文件.js
- 如何在视频中添加视频文件.js 4.2.1.
- 从快速节点下载文件.js具有 JWT 授权
- 访问 JavaScript 文件.js中的 C# 变量
- 在节点中热重载外部 js 文件.js如果文件有任何更改
- 如何在一台服务器中运行不同的.js文件.js NODE JS中的文件
- 如何在多文件.js中上传文件时检查文件扩展名
- 将文件.js中的var导入到模块joomla的模板中,并使用后台进行修改
- 在path项目中编写文件js脚本