带有参数的Jquery函数
Jquery function with arguments
这里我有一个函数,它将在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) {
相关文章:
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 如何在JQuery函数中定义一个值,然后调用另一个函数并使用该值
- 如何从jquery函数返回变量
- 如何将具有文本类型值的var放入jQuery函数中
- Jquery函数在错误的时间提交
- 锚链接无法使用.slideUp jquery函数
- 重新分解jQuery函数
- 如何在视图模型contet更新更新上调用Jquery函数
- 如何向这个javascript/jquery函数添加参数
- 添加类来触发jquery函数
- 更改智能表中的页面将停止JQuery函数的工作
- 使用每500ms运行一次的jquery函数是个好主意吗
- jQuery函数不能只在一个页面上工作
- 如何将jquery函数链接到vanilla选择器
- 仅在小型设备上调用jQuery函数
- 如何将jquery函数仅应用于大屏幕
- 奇怪的jquery函数行为
- Backbone listenTo不将jquery函数作为处理程序进行激发
- 在jquery函数内部设置来自jquery函数的var;t运行
- 从Jquery函数中获取一个变量