2015-09-28 56 views
1

如何從使用JavaScript的文本文件中提取文本數據?我正在製作一本在線字典。我可以編寫HTML和CSS,但不是JavaScript。使用javascript從文本文件中提取數據

單詞將在第一個文本框中輸入,然後搜索按鈕將被點擊,結果將顯示在底部的文本框中。 文本文件中的數據類似於「apple | a juicy fruit」 (不含引號「 現在我希望搜索字段中的文本檢查」|「符號前面的單詞,如果發現匹配單詞/單詞後面的」| | ?「將被髮送到結果字段 我怎樣才能做到這一點使用JavaScript

我對項目的HTML代碼如下:

<html> 
    <head> 
    <title>Online Dictionary</title> 
    <style> 
     .field{ 
     border: 2px solid black; 
     } 
     #result_field{ 
     margin-top: 5px; 
     width: 247px; 
     height: 100px; 
     } 
    </style> 
    </head> 
    <body> 
    <input type="text" placeholder="Enter your word here..." name="search_field" id="search_field" class="field"> 

    <input type="button" value="SEARCH" name="btn" id="btn" class="btn"> <br> 

    <input type="text" placeholder="Meaning here..." name="result_field" id="result_field" class="field"> 

    </body> 
</html> 
+1

,你將不得不使用PHP –

+1

要讀取本地文本文件? –

+1

澄清要求..你打算在客戶端文件搜索或在服務器端文件搜索? –

回答

0

嘗試使用Ajax

Ajax是什麼做。從服務器請求文件,因此您可以:

  • 檢查用戶網絡速度;
  • 獲取文件內容。

請參閱this

我做了幾個簡單的功能,你可以檢查他們here.

要使用的功能之一以上可以添加行這樣的:你想要求一個情況:

$GET('file.txt?c=0',function(result){/* Success function */},function(e){/* Error function */}); 

$GET('dictionary.txt?anyvalue=0',function(text){alert(text);},function(e){/* Error function */}); 

/* in ahead of the link you will have to include the ?urlvar=value to the function works, */ 
/*  I still didn't leave the function complete */ 

註文件從另一臺服務器然後該文件必須允許服務器訪問它(允許或不允許您可以使用PHP或htaccess)。

正如@Stephan Bijzitter所說,你可以使用PHP(或ASP) - 你可以很容易地得到一個單詞的正確含義。

例子是:

if($_GET['word']=="PC"){echo "Meaning";} 
switch($_GET['word']){case "Word":echo "A word?";break;} 
/* In PHP, $_GET['word'] returns the value of a parameter in page URL called "word" */ 
+0

我想我可以用這些位來製作我的字典。 –

+0

別人也推薦我Ajax。 –