English 中文(简体)
• 如何使用MVC3模式中的javascript
原标题:How to call javascript in MVC3 pattern

I m New to the ASP. NET MVC 3型式,我想说 Java本功能(或控制器),以显示结果为<代码>div或span

My solution

  1. I create a new ASP MVC3 Solution (using Razor)
  2. Add a new JavaScript in Script folder
  3. 在新的 Java文本中添加一个简单的文字功能,如:

    function HolaMundo(){
    return "hola desde javascript";
    }
    
  4. 在我的违约控制人档案中添加“Java”字句中的参考资料:index.cs/2006/2/code>。

    @section JavaScript
    {
        <script src="@Url.Content("~/Scripts/Maps.Helper.js")" type="text/javascript">  
        </script>
    }
    
  5. 最后,要求履行以下职能:

    <p>
        <div id="map_canvas2" style = "width:100%; height:100%">
            result from Javascript
        </div>
    </p>
    <script type="text/javascript">
        $(document).ready(function() {
         <text>
        $( #map_canvas2 ).text(HolaMundo());
         </text>
        });
    </script>
    

Result

工作


So, can any help me? I appreciate your help.

最佳回答

您应删除“<代码><text> tags from theirscript,以便:

<p>
    <div id="map_canvas2" style = "width:100%; height:100%">
        result from Javascript
    </div>
</p>

<script type="text/javascript">
    $(document).ready(function() {
        $( #map_canvas2 ).text(HolaMundo());
    });
</script>

The <text> tag is used by the Razor interpreter to indicate a literal value to be rendered as-is without processing. It s useful when you are mixing server side code with client side markup or script. For example:

<script type="text/javascript">
    $(document).ready(function() {
        @if (Model.IsFooBar)
        {
            <text>$( #map_canvas2 ).text(HolaMundo());</text>
        }
    });
</script>

还确保你在<条码>内得/编码>中界定的章节。 观点:

@section JavaScript
{
    <script src="@Url.Content("~/Scripts/Maps.Helper.js")" type="text/javascript"></script>
}

您应确保在您的<代码>上登记。 例如,见<代码><head>。 3. 还确保你把 j写在你的网页上:

<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>
    @RenderSection("JavaScript")
</head>
...

甚至在封闭式<代码></上>的标签之前,建议贴上文字,避免阻挡在电离层中载荷的浏览器:

    ...
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    @RenderSection("JavaScript")
</body>
</html>
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签