• mvc 注册脚本的其他方法


    privatestaticSortedList<int,string>GetRegisteredScriptIncludes()
    {
       
    var registeredScriptIncludes =System.Web.HttpContext.Current.Items["RegisteredScriptIncludes"]asSortedList<int,string>;

       
    if(registeredScriptIncludes ==null)
       
    {
            registeredScriptIncludes
    =newSortedList<int,string>();
           
    System.Web.HttpContext.Current.Items["RegisteredScriptIncludes"]= registeredScriptIncludes;
       
    }

       
    return registeredScriptIncludes;
    }

    publicstaticvoidRegisterScriptInclude(thisHtmlHelper htmlhelper,string script)
    {
       
    var registeredScriptIncludes =GetRegisteredScriptIncludes();
       
    if(!registeredScriptIncludes.ContainsValue(script))
       
    {
            registeredScriptIncludes
    .Add(registeredScriptIncludes.Count, script);
       
    }
    }

    publicstaticstringRenderScripts(thisHtmlHelper htmlhelper)
    {
       
    var registeredScriptIncludes =GetRegisteredScriptIncludes();
       
    var scripts =newStringBuilder();
       
    foreach(string script in registeredScriptIncludes.Values)
       
    {
            scripts
    .AppendLine("<script src='"+ script +"' type='text/javascript'></script>");
       
    }
       
    return scripts.ToString();
    }

    然后
    <%
       
    Html.RegisterScriptInclude(Url.Content("~/Scripts/my.js"));
    %>

    最后在body中相应的位置添加下面这句

    <%=Html.RenderScripts() %>



    ///////////////////////////////////////////////
    直接通过 javascript 来注册脚本
    <scripttype="text/javascript"src="somescript.js"></script>
    相当于
    var script = document.createElement('script');
    script
    .type ='text/javascript';
    script
    .src ='somescript.js';
    $
    ('#someElement').append(script);

  • 相关阅读:
    php笔记之数组
    php笔记之函数
    vue笔记之路由
    <QT>:使用QUdpSocket进行UDP通信
    Linux下用ssh在远程Xserver上显示图形界面
    <学习QT>在QListWidget中实现每个单元项显示文字在上图片在下
    const限定符以及顶层const和底层const的理解
    了解X window以及相关知识
    Linux下使用Eclipse C/C++生成创建并调用动态库(.so)
    Linux下搭载Xwindow(Xlib)开发环境
  • 原文地址:https://www.cnblogs.com/iwaitu/p/2329110.html
Copyright © 2020-2023  润新知