带有参数的Jquery函数

Jquery function with arguments

本文关键字:Jquery 函数 参数      更新时间:2023-09-26

这里我有一个函数,它将在jquery工具提示中显示基于div对象宽度的时间。

现在我想用同一个函数来计算"开始时间"answers"结束时间",但我想用同样的函数,而不是写新的,所以我必须创建这个函数来处理参数,但从逻辑上讲,我真的不知道如何实现它。

SO:在工具提示中,我必须显示:

var time = $( this ).width();
 var startTime = $( this ).position().left;
 var endTime = startTime + time;

这里还有工具提示代码:

$(document).tooltip({
        items: ".draggable",
        track: true,
        content: updateTooltipContent
    });

和函数,我必须在其中添加参数:

function updateTooltipContent() {
    var time = $(this).width();
    if (time < 0) {
        time = 0;
    }
    var seconds = time % 60;
    var minutes = (time - seconds) / 60;
    var output;
    if (minutes >= 10) {
        output = "" + minutes;
    } else {
        output = "0" + minutes;
    }
    output += "h ";
    if (seconds >= 10) {
        output += seconds;
    } else {
        output += "0" + seconds;
    }
    return output + "min";
}
});

现在工具提示必须显示:"spending time" + time + "start time" + startTime + "end time" + endTime;

如何使这个带有参数的函数使用3次,因为我必须将数字转换为时间格式?

我真的不明白你在问什么,但如果你只是需要"updateTooltipContent"函数来使用时间格式化程序,你应该这样做:

$(document).tooltip({
    items: ".draggable",
    track: true,
    content: function() {
      return formatTime($(this).width()) + formatTime(whatever) + formatTime(somethingElse);
    }
});
function formatTime( time ) {
    if (time < 0) {
        time = 0;
    }
    var seconds = time % 60;
    var minutes = (time - seconds) / 60;
    var output;
    if (minutes >= 10) {
        output = "" + minutes;
    } else {
        output = "0" + minutes;
    }
    output += "h ";
    if (seconds >= 10) {
        output += seconds;
    } else {
        output += "0" + seconds;
    }
    return output + "min";
}

从元素的宽度中获取时间值似乎很奇怪,但不管怎样。

你的意思是:

$(document).tooltip({
    items: ".draggable",
    track: true,
    content: updateTooltipContent(a, b)
});
function updateTooltipContent(a, b) {