简体   繁体   English

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

[英]Why is my javascript not executed within this Razor code?

I have some working Raphael-js code from this tutorial: http://net.tutsplus.com/tutorials/javascript-ajax/an-introduction-to-the-raphael-js-library/ and I'm trying to include the working code in to an MVC3 Razor view. 我在本教程中有一些工作的Raphael-js代码: http//net.tutsplus.com/tutorials/javascript-ajax/an-introduction-to-the-raphael-js-library/我试图包括将代码转换为MVC3 Razor视图。 The javascript isn't getting executed. javascript没有被执行。 I can't seem to find the error or a good resorce which explains how to include javascript within Razor. 我似乎无法找到错误或良好的resorce解释如何在Razor中包含JavaScript。 Here is my .cshtml where I expect the Rapheael drawing to be rendered at the canvas_container div. 这是我的.cshtml,我希望Rapheael绘图在canvas_container div中呈现。

@{
    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>
}

And here is the rendered view where the javascript is not executed: 这里是未执行javascript的渲染视图:

<!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>

Thanks Scott 谢谢斯科特

It's not working because your javascript follows a self-closed script tag, which is invalid . 它不起作用,因为你的javascript遵循自我关闭的script标记,这是无效的

You need to change 你需要改变

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM