如何在 mvc3 中的页面加载时调用 javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7306871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to call javascript function on page load in mvc3
提问by Bhargav
I have a javascript function
我有一个 javascript 函数
function relativeTime(time) {
var period = new Date(time);
var delta = new Date() - period;
if (delta <= 10000) { // Less than 10 seconds ago
return 'Just now';
}
var units = null;
var conversions = {
millisecond: 1, // ms -> ms
second: 1000, // ms -> sec
minute: 60, // sec -> min
hour: 60, // min -> hour
day: 24, // hour -> day
month: 30, // day -> month (roughly)
year: 12 // month -> year
};
for (var key in conversions) {
if (delta < conversions[key]) {
break;
}
else {
units = key;
delta = delta / conversions[key];
}
}
// Pluralize if necessary:
delta = Math.floor(delta);
if (delta !== 1) { units += 's'; }
return [delta, units, "ago"].join(' ');
}
that give relative time difference like facebook comment.
这给出了像facebook评论这样的相对时间差。
How can i call this function in my view. I am using mvc3. i am getting time from database like,
在我看来,我如何调用这个函数。我正在使用 mvc3。我正在从数据库中获取时间,例如,
<span>
@item.wallTimeStamp
</span>
instead of that i want to call javascript function,
而不是我想调用javascript函数,
i have a tag
我有一个标签
<span>
//call javascript function that will display time difference in this tag
</span>
how can i do that?
我怎样才能做到这一点?
采纳答案by archil
First, give the span id(or something else for identification)
首先,给出span id(或其他用于识别的东西)
<span id="relativeTime">
</span>
Then, using javascript, first calculate that relative time value using your function. Then, using jQuery.text(), set that value inside span
然后,使用 javascript,首先使用您的函数计算该相对时间值。然后,使用jQuery.text(),在 span 内设置该值
<script>
$(document).ready(function() {
var wallTimeStamp = '@item.wallTimeStamp';
var relativeTimeValue = relativeTime(wallTimeStamp);
$('#relativeTime').text(relativeTimeValue);
});
</scipt>
Code modification may be needed for multiple spans of that kind
这种类型的多个跨度可能需要修改代码
回答by Bozho
$(document).ready(function() {
//called when the document is ready
});
回答by Nicola Peluchetti
回答by mnsr
<body onload="javascript:relativeTime(time);">
where time
is whatever you want it to be.
time
无论你想要它在哪里。
If you want it as a clickable thing with an assigned value from your model, then do something like
如果您希望将其作为具有模型指定值的可点击内容,请执行以下操作
<span onclick="relativeTime(@item.time)">@item.wallTimeStamp</span>
edit:
编辑:
alright..
好吧..
assuming your time in the function is coming from your model:
假设您在函数中的时间来自您的模型:
@HiddenFor(m => m.time)
create a seperate js file, and link it in your html doc:
创建一个单独的 js 文件,并将其链接到您的 html 文档中:
<script type="text/javascript" src="whatever.js"></script>
inside it (assuming you mean jquery):
在它里面(假设你的意思是 jquery):
$(function() {
relativeTime($("#time").val());
});