js中定義了一個move(first,modify)方法,需要傳入first,modify。因為我想重復使用move函數可是有的地方不需要modify,有的地方需要,傳空值的話也不行,無法運行。有什么方法能不需要傳入固定個數的參數運行嘛。調用function moveTest() { var first = document.getElementById(id); var modify = { y: function () { statements } }; move(first, modify); //這里一定要兩個參數不然運行不了。}方法 function move(first, modify) { first.onmousedown = function (e) { //把onclick改成onmouseover就是一獲得焦點圖片就跟隨鼠標移動,onmousedown鼠標拖動效果 modify.y(); //方法 var x = e.clientX - first.offsetLeft; var y = e.clientY - first.offsetTop; document.onmousemove = function (e) { var left = e.clientX - x; var top = e.clientY - y; first.style.left = left + 'px'; first.style.top = top + 'px'; document.onmouseup = function () { //鼠標松開時釋放函數,如果需要拖至指定位置可在此添加運算邏輯; document.onmousemove = null; document.onmouseup = null; } } },
js構造了一個函數,內部有兩個變量,我調用的時候可以只傳一個進去嗎。
慕碼人8056858
2019-03-07 14:13:21
