繁体   English   中英

如何在 cshtml 文件中调用外部 javascript 文件

[英]How do i call external javascript file in cshtml file

下面是我的 Javascript 文件。

复制.js

function CopyYourSheet() {

    var ifrm = document.createElement("iframe");
    ifrm.setAttribute("src", "https://docs.google.com/spreadsheets/d/1992ry0dpIjel_TQuB-W6fb2Nd7uST69AX_jY/edit#");
    ifrm.style.width = "10000px";
    ifrm.style.height = "2000px";
    document.body.appendChild(ifrm);
    }

下面是 CSHTML 文件

索引.cshtml

@{
    ViewData["Title"] = "Home Page";
}

    <div class="text-center">
        <h1 class="display-4">Welcome</h1>                    
        <button id="CopyYourSheet" onclick="CopyYourSheet()">Get your details</button>
    </div>

包含它的正确方法是将 Copy.js 捆绑到您的包中。

在 App_Start 文件夹下打开 BundleConfig

然后加

bundles.Add(new ScriptBundle("~/bundles/myjs").Include(
                        "~/Scripts/copy.js"));

然后像这样将它包含在您的主视图中

@Scripts.Render("~/bundles/myjs")

如果您没有 App_Start 文件夹,或者您不喜欢捆绑到 MVC 项目中,则将脚本包含在 _Layout 局部视图的<head>标记内。 最好将它添加到结束</head>标记的正上方。

<script src="~/Scripts/copy.js"></script>

确保文件的路径正确。 如果 js 文件位于主项目树中,您可能需要删除 src 路径中的 /Scripts/ 文件夹。 ~ 应该保留在路径的开头,以便在从不同环境运行代码时使其灵活。

暂无
暂无

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

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