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

使用javascript从文本文件中提取数据,javascript,html,css,Javascript,Html,Css,如何使用javascript从文本文件中提取文本数据?我正在制作一本在线词典。我能很好地编写HTML和CSS,但不能编写javascript 单词将在第一个文本框中输入,然后单击搜索按钮,结果将显示在底部文本框中。 文本文件中的数据就像“苹果|一种多汁的水果” (不含报价) 现在,我希望搜索字段中的文本检查“|”符号之前的单词,如果找到匹配项,“|”之后的单词/单词将发送到结果字段。 如何使用javascript实现这一点 我的项目HTML代码如下: <html> <hea

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

单词将在第一个文本框中输入,然后单击搜索按钮,结果将显示在底部文本框中。 文本文件中的数据就像“苹果|一种多汁的水果” (不含报价) 现在,我希望搜索字段中的文本检查“|”符号之前的单词,如果找到匹配项,“|”之后的单词/单词将发送到结果字段。 如何使用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>

在线词典
.场{
边框:2件纯黑;
}
#结果_字段{
边缘顶部:5px;
宽度:247px;
高度:100px;
}

尝试使用Ajax

Ajax是从服务器请求文件的工具,因此您可以使用它:

  • 检查用户网络的速度
  • 获取文件的内容

我做了几个简单的函数,你可以检查它们

要使用上述函数之一,可以添加如下行:

$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" */
尝试使用Ajax

Ajax是从服务器请求文件的工具,因此您可以使用它:

  • 检查用户网络的速度
  • 获取文件的内容

我做了几个简单的函数,你可以检查它们

要使用上述函数之一,可以添加如下行:

$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" */

您必须使用phpy您想读取本地文本文件吗?需要澄清。您是要在客户端的文件中搜索还是在服务器端的文件中搜索?这取决于堆栈的其余部分。您是使用MEAN还是…?这可能会帮助您:文本文件将在服务器端。您必须使用phpy您想读取local文本文件?需要澄清..你是要在客户端的文件中搜索还是在服务器端的文件中搜索?这取决于堆栈的其他部分..你是使用MEAN还是…?这可能会帮助你:文本文件将在服务器端。我想我可以使用这些位来制作字典。其他人也推荐我使用Ajax.我想我可以用这些东西来编我的字典.还有人推荐我使用Ajax。