Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在单击按钮时运行函数?_Javascript_Jquery - Fatal编程技术网

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>