Javascript 如何在单击按钮时运行函数?
如果单击按钮,如何运行此功能Javascript 如何在单击按钮时运行函数?,javascript,jquery,Javascript,Jquery,如果单击按钮,如何运行此功能 <button id="button">Button</button> var tween = new TWEEN.Tween(mesh.scale).to({ x: 1, y: 0.05, z:2 }, 1000).start(); tween.easing(TWEEN.Easing.Elastic.InOut); tween.repeat(Infinity); tween.yoyo(true); 按钮 var tween=new
<button id="button">Button</button>
var tween = new TWEEN.Tween(mesh.scale).to({ x: 1, y: 0.05, z:2 }, 1000).start();
tween.easing(TWEEN.Easing.Elastic.InOut);
tween.repeat(Infinity);
tween.yoyo(true);
按钮
var tween=new tween.tween(mesh.scale).to({x:1,y:0.05,z:2},1000).start();
吐温缓和(吐温缓和弹性输入);
重复(无限);
tween.yoyo(真的);
使用jQuery:
$('#button').click(function(){
var tween = new TWEEN.Tween(mesh.scale).to({ x: 1, y: 0.05, z:2 }, 1000).start();
tween.easing(TWEEN.Easing.Elastic.InOut);
tween.repeat(Infinity);
tween.yoyo(true);
return false; //if you want the button to not process anything further
});
按钮
函数functionToCall(){
// ...
}
或
按钮
//普通JS
document.getElementById('button')。addEventListener('click',function(){
// ...
});
//jQuery
$(“#按钮”)。单击(函数(){
// ...
});
这里的问题是,您希望在单击按钮时能够运行JavaScript代码。解决方法是创建一个函数,然后将按钮的“onclick”属性设置为函数名。像这样:
<button id="button" onclick="functionName()">Button</button>
<script>
function functionName ()
{
var tween = new TWEEN.Tween(mesh.scale).to({ x: 1, y: 0.05, z:2 }, 1000).start();
tween.easing(TWEEN.Easing.Elastic.InOut);
tween.repeat(Infinity);
tween.yoyo(true);
}
</script>
按钮
函数名()
{
var tween=new tween.tween(mesh.scale).to({x:1,y:0.05,z:2},1000).start();
吐温缓和(吐温缓和弹性输入);
重复(无限);
tween.yoyo(真的);
}
你试过了吗?
<button id="button">Button</button>
<script>
// Plain JS
document.getElementById('button').addEventListener('click', function() {
// ...
});
// jQuery
$('#button').click(function() {
// ...
});
</script>
<button id="button" onclick="functionName()">Button</button>
<script>
function functionName ()
{
var tween = new TWEEN.Tween(mesh.scale).to({ x: 1, y: 0.05, z:2 }, 1000).start();
tween.easing(TWEEN.Easing.Elastic.InOut);
tween.repeat(Infinity);
tween.yoyo(true);
}
</script>