MVC3 Razor: call javascript function from view

后端 未结 2 1500
忘掉有多难
忘掉有多难 2021-01-04 19:30

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

相关标签:
2条回答
  • 2021-01-04 20:12

    The last line of the datum function should return the time rather than setting the innerHTML:

    return 'Jetzt ist: ' + stunden + ':' + minuten + ':' + sekunden;
    

    and your HTML can then be something like this:

    The time is: <script type="text/javascript">datum(uhr)</script>
    
    0 讨论(0)
  • 2021-01-04 20:25

    You should move the code from _Layout.cshtml into a seperate .js file and add a reference to it.

    Also, you should change the index.cshtml code to be this:

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

    JS Fiddle Example

    0 讨论(0)
提交回复
热议问题