如何将按钮单击转换为单独的 js 文件中的鼠标向上事件



如何在单独的js文件中将按钮单击转换为鼠标向上事件。

我在 html 文件中有这个按钮代码(可以正常工作(:

<button class="J_sheetControl" id="J_timingSubmit2">Submit</button>

单击时,它会在外部js文件中运行一个函数(?(:

$("#J_timingSubmit2").click(function(ev){
var sheetStates = sheet.getSheetStates();
var rowsCount = 15;
var colsCount = 7;
var timesheetrowsdata = "";
var timesheetcoldata = "";
for(var row= 0, rowStates=[]; row<rowsCount; ++row){
rowStates = sheetStates[row];
timesheetrowsdata += rowStates+(row==rowsCount-1?'':',');
}
timesheetcoldata = timesheetrowsdata.replace(/,/g, '');
const testData = timesheetcoldata;
const dataArr = testData.match(/.{1,7}/g)
.map(s => Number(s[0])) // Only take the first char as a Number
let dataSum = dataArr.reduce((a, b) => a + b);
let isSameAsRowsCount = dataSum == rowsCount;
});

我想转换它,以便它在单击表标题(在鼠标向上事件(时运行。 此函数位于第二个 js 文件中(函数调用需要插入 if 语句的末尾(:

thisSheet.delegate(".TimeSheet-colHead","mouseup.umsSheetEvent",function(ev){
if(!operationArea.startCell){
return;
}
var curColHead = $(ev.currentTarget);
const targetStateValue = localStorage.getItem('shiftstatus');
if (targetStateValue === "earlyshift") {
var endCell = [14,curColHead.data("col")];
var correctedCells = cellCompare(operationArea.startCell,endCell);
afterSelecting(ev,correctedCells);
//code needs to go here                               
}

我试图重命名它,使其看起来更像一个标准函数,例如 ColumnChecker(( 但这只会给我错误,它不是上述鼠标向上事件的功能。

我已经解决了这个问题,但这似乎不是最整洁的方式(我已经在应用程序中使用了两次类似的方法(。如果没有人有更好的解决方案,我会用它作为答案。

var but_Open3 = document.getElementById("J_timingSubmit2");
J_timingSubmit2.click(); 

使用 jQuery,您可以为鼠标向上事件添加另一个事件侦听器。

$("#J_timingSubmit2").mouseup( handler);
$("#J_timingSubmit2").on('mouseup', handler);

如果没有解决方法,这似乎是不可能的。 我隐藏了按钮:

<button class="J_sheetControl" id="J_timingSubmit2" style="display:none;">Open Hidden>Submit</button>

然后,我在鼠标向上事件中调用按钮(原始问题(:

J_timingSubmit2.click();

然后运行上面详述的函数:

$("#J_timingSubmit2").click(function(ev){

最新更新