使用javascript从文本文件中提取数据

时间:2015-09-28 17:26:00

标签: javascript html css

如何使用javascript从文本文件中提取文本数据?我正在制作在线词典。我可以很好地编写HTML和CSS代码,但不能编写javascript。

Word将在第一个文本框中输入,然后单击搜索按钮,结果将显示在底部文本框中。 文本文件中的数据就像“苹果|多汁的水果”                               (没有引文“ 现在我希望搜索字段中的文本检查“|”之前的单词签名,如果匹配,在“|”后找到单词/单词将被发送到结果字段。 我怎么能用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 个答案:

答案 0 :(得分: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" */