下面是我的 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 项目中,请将该脚本包含在 <head>
中。 _Layout 部分 View 的标签。将其添加到结束符 </head>
的正上方标签是最好的。
<script src="~/Scripts/copy.js"></script>
确保文件路径正确。如果 js 文件位于主项目树中,您可能需要删除 src 路径中的/Scripts/文件夹。 ~ 应保留在路径的开头,以便在从不同环境运行代码时保持灵活性。
关于javascript - 如何在cshtml文件中调用外部javascript文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61671502/