我无法在事件之外指定变量

时间:2018-12-13 06:43:19

标签: javascript

我无法在需要帮助的范围内获得a和b

我需要找到c来完成我的代码和想法

.html

<div id ='hours'></div>

.js

var h = document.getElementById('hours');
   h.onmousedown = (e) => {var a = e.clientX; console.log('in' ,e.clientX);} 
            h.onmouseup = (e) =>{var b = e.clientX; console.log('out' , e.clientX); }
            var c = a - b ;

1 个答案:

答案 0 :(得分:1)

在代码的开头定义ab

   var h = document.getElementById('hours');
   var a,b;
   h.onmousedown = (e) => {a = e.clientX; console.log('in' ,e.clientX);} 
   h.onmouseup = (e) =>{b = e.clientX; console.log('out' , e.clientX); }
   var c = a - b ;