MVC3 Razor:从视图中调用 javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7443263/
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
MVC3 Razor: call javascript function from view
提问by MagB
I am new in MVC3 Razor and want to show running time on a view (index.cshtml). I use a javascript function and put it in _Layout.cshtml so all other "home" views can use it (see following snippet)
我是 MVC3 Razor 的新手,想在视图 (index.cshtml) 上显示运行时间。我使用一个 javascript 函数并将其放在 _Layout.cshtml 中,以便所有其他“主页”视图都可以使用它(请参阅以下代码段)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>
@ViewBag.Title
</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
</head>
<script type="text/javascript">
var uhr = new Date();
var minuten;
var stunden;
var sekunden;
var interval = 500;
function datum(id) {
uhr.setTime(uhr.getTime() + interval);
window.setTimeout(function () { datum(id) }, interval);
minuten = uhr.getMinutes();
stunden = uhr.getHours();
sekunden = uhr.getSeconds();
if (minuten < 10) { minuten = '0' + minuten; }
if (sekunden < 10) { sekunden = '0' + sekunden; }
if (stunden < 10) { stunden = '0' + stunden; }
document.getElementById(id).innerHTML = 'Jetzt ist: ' + stunden + ':' + minuten + ':' + sekunden;
}
</script>
My questions: 1. how can I call this function (e.g. datum("uhr")) from index.cshtml? My try (see following) doesn't work:
我的问题: 1. 我如何从 index.cshtml 调用这个函数(例如 datum("uhr"))?我的尝试(见下文)不起作用:
@section SideBar {
<p>
<ul>
<li><a href="Index.cshtml">Add Job</a></li>
<li><a href="About.cshtml">About</a></li>
</ul>
</p>
<p>
The time is: datum("uhr");
</p>
}
- Any other "better" way?
- Is it a good practice? I am not sure if it is correct putting javascript function in _Layout.cshtml.
- 还有其他“更好”的方式吗?
- 这是一个好习惯吗?我不确定将 javascript 函数放在 _Layout.cshtml 中是否正确。
Thx in advance.
提前谢谢。
回答by Richard Dalton
You should move the code from _Layout.cshtml into a seperate .js file and add a reference to it.
您应该将代码从 _Layout.cshtml 移动到一个单独的 .js 文件中并添加对它的引用。
Also, you should change the index.cshtml code to be this:
此外,您应该将 index.cshtml 代码更改为:
@section SideBar {
<p>
<ul>
<li><a href="Index.cshtml">Add Job</a></li>
<li><a href="About.cshtml">About</a></li>
</ul>
</p>
<p>
The time is: <span id="uhr"></span>
</p>
<script type="text/javascript">datum("uhr");</script>
}
回答by soniiic
The last line of the datum function should return the time rather than setting the innerHTML:
datum 函数的最后一行应该返回时间而不是设置innerHTML:
return 'Jetzt ist: ' + stunden + ':' + minuten + ':' + sekunden;
and your HTML can then be something like this:
然后你的 HTML 可以是这样的:
The time is: <script type="text/javascript">datum(uhr)</script>