【问题标题】:Jquery function with arguments带参数的 Jquery 函数
【发布时间】:2013-07-18 18:43:42
【问题描述】:

这里我有一个函数可以在 jquery tooltip 中根据 div 对象的宽度显示时间。

现在我想用同一个函数来计算“开始时间”和“结束时间”,但我想用同一个函数,而不是写新的,所以我必须创建这个函数来带参数,但逻辑上我真的不明白如何让它发生。

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;

如何使这个函数与 argment 一起使用 3 次,因为我必须将数字转换为时间格式?

【问题讨论】:

  • 你是什么意思,“有论据”?什么论据?论据是什么?
  • 函数(时间、开始时间、结束时间)...
  • 我可以写 3 个功能,但为什么我只能写一个..
  • 你会写哪三个函数?
  • 函数更新() var starTime ...函数更新1 var endTime ...

标签: javascript jquery function arguments datetime-format


【解决方案1】:

你的意思是:

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

function updateTooltipContent(a, b) {

【讨论】:

  • 是的,我知道,但是功能如何,如何将 start 和 endTime 添加到功能中......在哪里?请写广告 jsfiddle
  • 我可以写 3 个函数,但想尝试一个带参数的函数,对不起我的英语
  • 我 3 次需要将数字转换为时间格式,那么为什么要编写 3 个不同的函数来完成相同的工作
【解决方案2】:

我真的不明白你在问什么,但如果只是你需要“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";
}

从元素的宽度获取时间值似乎很奇怪,但无论如何。

【讨论】:

    猜你喜欢
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 2013-10-27
    • 2011-08-07
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多