在事件单击时传递函数之间的var

时间:2018-03-14 00:29:21

标签: javascript

document.getElementById('button').addEventListener('click', function() {
ApiCall()
console.log(lol)

});



function ApiCall() {
var lol = "this is a test";
}

我已经查看了javascript范围但是如何将此变量传递给第一个函数?

1 个答案:

答案 0 :(得分:2)

您应该从函数中返回值。像这样:

document.getElementById('button').addEventListener('click', function() {
  var passed_lol = ApiCall()
  console.log(passed_lol)
});

function ApiCall() {
  var lol = "this is a test"
  return lol;
}
<input id="button" type="button" onclick=stop() value="clickme">