javascript 在java脚本中从文本文件中搜索字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24231617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Search for a string from a text file in java script
提问by Dilip wk
I am creating a Dictionary app for mobile using Java Script. I have stored Dictionary source in a separate file. My doubt is how to access that file and search for particular string using Java Script.
我正在使用 Java 脚本为移动设备创建字典应用程序。我将字典源存储在一个单独的文件中。我的疑问是如何使用 Java Script 访问该文件并搜索特定字符串。
function dic() {
var word = document.getElementById("wo").value;
var dictionary = new Array("Java","Python","Swift","HTML","PHP");
var flag = 0;
for(var i = 0; i < dictionary.length; i++) {
if(dictionary[i] == word) {
document.getElementById("result").innerHTML = "Element Found";
flag = 1;
break;
}
}
if(flag == 0)
document.getElementById("result").innerHTML = "Element Not Found";
}
This script checks for the word in array. I want to replace the array words to a text file. Thanks in advance
此脚本检查数组中的单词。我想将数组单词替换为文本文件。提前致谢
回答by ArtOfCode
To access the file, use jQuery's .get()
function. This can also be done with XMLHttpRequests
in pure JS but jQuery will handle cross-browser compatibilty for you.
要访问该文件,请使用 jQuery 的.get()
函数。这也可以XMLHttpRequests
在纯 JS 中完成,但 jQuery 将为您处理跨浏览器兼容性。
To get the file:
要获取文件:
$.get("http://www.example.com/path/to/file.txt",function(returnedData) {
$("#element").text(returnedData);
},"text/plain");
This will load the contents of http://www.example.com/path/to/file.txtinto the div
with ID element
. Assuming each dictionary word is on a new line, you can then separate them into an array with the following:
这会将http://www.example.com/path/to/file.txt的内容加载到div
with ID 中element
。假设每个字典单词都在一个新行上,然后您可以将它们分成一个数组,如下所示:
var text = $("#element").text();
var words = text.split("\n");
var dictionary = new Array();
for(var i=0; i < words.length; i++) {
dictionary[i] = words[i];
}
Now you have the dictionary words in your array and can search for them with your existing code (with a couple of corrections):
现在您的数组中有字典单词,并且可以使用现有代码搜索它们(进行一些更正):
function dic() {
var word = document.getElementById("wo").value;
// var dictionary = new Array("Java","Python","Swift","HTML","PHP");
// This line is unnecessary; use the dictionary var already created.
var flag = 0;
for(var i = 0; i < dictionary.length; i++) {
if(dictionary[i] == word) {
document.getElementById("result").innerHTML = "Element Found";
flag = 1;
break;
}
if(i == dictionary.length - 1) {
document.getElementById("result").innerHTML = "Element Not Found";
}
}
}
That should leave you with the right answer.
这应该给你留下正确的答案。
Hope this helps.
希望这可以帮助。
EDIT:
Part of the code in my second code sample is unnecessary and only there for clarity; you can in fact just state that var dictionary = text.split("\n");
, which will yield the same result.
编辑:
我的第二个代码示例中的部分代码是不必要的,只是为了清楚起见;实际上,您可以仅声明var dictionary = text.split("\n");
,这将产生相同的结果。