2017-07-19 54 views
-1

所以在我的JavaScript遠JavaScript變量,我有這樣的代碼: !/Users/me/Desktop/Screen Shot 2017-07-19 at 2.15.40 PM.png存儲在HTML

@import "MochaJSDelegate.js"; 
@import 'common.js'; 

var word = "some kind of file"; 
function getWord(){ 
    return word; 
} 

而且我希望能夠在這裏使用「字」,變量在我的html:

enter image description here

<!doctype html> 
<html> 
    <head> 
    <title></title> 
     <link rel="stylesheet" href="style3.css"> 
     <script src="https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js"></script> 
     <script src = "../Sketch/login.js"> </script> 
    </head> 


    <body> 



    <div> 

     <div class = "title"> 
      Botworx Sketch Plugin 
     </div> 


     <div class = "input-login"> 
      <button onclick="displayFormContents(this.form);">Login</button> 
     </div> 

     <div class = "input-uploadImages"> 
      <button onclick="displayFormContents(this.form);">Upload Images Only</button> 
     </div> 
     <div class = "input-uploadGalleryData"> 
      <button id = "upload-gallery">Upload Gallery Data</button> 
     </div> 
     <div class = "input-uploadAll" > 
      <button id="uploadEverything">Upload Everything</button> 
     </div> 
     <input type="file" id="file-chooser" /> 



      <div id="results"></div> 

      <div id="fb-root"></div> 

     <button id="upload-button">Upload to S3</button> 




    </div> 

<script> 
    alert(getWord()); 
</script> 

的亮的行是我參考了JavaScript文件。當我使用標籤並嘗試在html中使用我的函數:「getWord()」時,沒有任何反應。我測試了一下,發現html不喜歡「import」和「@import」這兩個詞。我需要這些關鍵字在我的JavaScript文件中。

我的問題是如何使用@import訪問我的JavaScript文件?

+1

正如上文[如何提問] (https://stackoverflow.com/help/how-to-ask),你應該在代碼中包含你的代碼(不僅僅是代碼的圖片) – alexanderbird

+0

我在代碼中添加了代碼!對於那個很抱歉。 –

回答

0

不知道你的要求的性質,它看起來像您可以使用庫這樣處理@importhttps://www.npmjs.com/package/grunt-import-js

在這個例子中,進口JS用來從 應用程序讀取所有的JavaScript文件/ Resources/js /並掃描它們以獲取@import指令。 @import文件的內容 然後替換@import指令。在<head>或在<body>標籤的底部,或者可能把它身體內部:

如果我們忽略@import,你可以添加到您的HTML文件略低於<body>標籤(編輯頂部。如果JavaScript是用來顯示或操作頁面上的任何東西):

<script type="text/javascript"> 
    var word = "some kind of file"; 
    function getWord(input){ 
    console.log(input); 
    } 
</script> 

然後,您可以訪問它,在你的HTML:

<div class = "input-login"> 
    <button onclick="getWord(word);">Login</button> 
</div>