簡體   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