2015-09-28 53 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。 –