gpt4 book ai didi

javascript - 为什么我的 javascript 没有在此 Razor 代码中执行?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:05:58 25 4
gpt4 key购买 nike

我有一些来自本教程的 Raphael-js 代码:http://net.tutsplus.com/tutorials/javascript-ajax/an-introduction-to-the-raphael-js-library/我正在尝试将工作代码包含到 MVC3 Razor View 中。 javascript 没有被执行。我似乎找不到解释如何在 Razor 中包含 javascript 的错误或好的资源。这是我的 .cshtml,我希望在 canvas_container div 中呈现 Rapheael 绘图。

@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">
http://asp.net/mvc</a>.
</p>

<div id="canvas_container">
</div>

@section JavaScript
{
<script type="text/javascript" src="@Url.Content("~/Scripts/raphael.js")" />
<script type="text/javascript">
window.onload = function () {
var paper = new Raphael(document.getElementById('canvas_container'), 500, 500);
var circle = paper.circle(100, 100, 80);
for (var i = 0; i < 5; i += 1) {
var multiplier = i * 5;
paper.circle(250 + (2 * multiplier), 100 + multiplier, 50 - multiplier)
}
var rectangle = paper.rect(200, 200, 250, 100);
var ellipse = paper.ellipse(200, 400, 100, 50);
}
</script>
}

这是未执行 javascript 的渲染 View :

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Home Page</title>
<link href="/Content/Site.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
</head>
<body>
<div class="page">
<header>
<div id="title">
<h1>MVC Movie App</h1>
</div>
<div id="logindisplay">
[ <a href="/Account/LogOn">Log On</a> ]
</div>
<nav>
<ul id="menu">
<li><a href="/">Home</a></li>
<li><a href="/Home/About">About</a></li>
</ul>
</nav>
</header>
<section id="main">
<h2>Welcome to ASP.NET MVC!</h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">
http://asp.net/mvc</a>.
</p>

<div id="canvas_container">
</div>
</section>
<footer>

</footer>
</div>
<script type="text/javascript" src="/Scripts/raphael.js" />
<script type="text/javascript">
window.onload = function () {
var paper = new Raphael(document.getElementById('canvas_container'), 500, 500);
var circle = paper.circle(100, 100, 80);
for (var i = 0; i < 5; i += 1) {
var multiplier = i * 5;
paper.circle(250 + (2 * multiplier), 100 + multiplier, 50 - multiplier)
}
var rectangle = paper.rect(200, 200, 250, 100);
var ellipse = paper.ellipse(200, 400, 100, 50);
}
</script>
</body>
</html>

谢谢斯科特

最佳答案

它不起作用,因为您的 javascript 遵循一个自闭的 script 标记,即 invalid .

你需要改变

<script type="text/javascript" src="/Scripts/raphael.js" />

<script type="text/javascript" src="/Scripts/raphael.js"></script>

关于javascript - 为什么我的 javascript 没有在此 Razor 代码中执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6448349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com