[英]How do I get an array to another function so I can display it into another div
I have this text file I am reading... I read it into an array. 我有这个正在阅读的文本文件...我把它读成数组。
fields[0]
is the term, fields[1]
is the definition and fields[2]
is the example. fields[0]
是术语, fields[1]
是定义, fields[2]
是示例。
I am trying to pass the array I get from the text file into another function so I can display the definition and example in another <div>
. 我试图将我从文本文件中获取的数组传递给另一个函数,以便我可以在另一个
<div>
显示定义和示例。 Here is my code: 这是我的代码:
<html>
<head>
<!--<input type="file" id="fileinput" /> -->
<script type="text/javascript">
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function allText()
{
if (xmlhttp.status == 200 && xmlhttp.readyState == 4)
{
var inputText = xmlhttp.responseText; // the whole text file being stored in a variable
var lines = inputText.split('\n'); // splitting the whole file into manageable rows using \n
var allMainText = ""; // declaring a variable for use later
for (var i = 0; i < lines.length; i++) // creating a for loop
{
var fields = lines[i].split('||'); // this is where the separation between tag, definition and example happens
// fields 0 is the tag itself
// fields 1 is the definition
// fields 2 is the example
var oneLine = '<a href = "#' + fields[0]
+ ' " onclick="testFunction();"> '
+ '<'
+ fields[0]
+ '>'
+ ' </a> ||';
allMainText += oneLine; // += means that things can be added as the loop executes
}
document.getElementById("menu").innerHTML = allMainText; // allMainText is displayed in the "main" div
}
}
function testFunction (fields, oneLine)
{
var defandExample = fields[1] + '<br/>' + fields[2];
document.getElementById("defandexample").innerHTML = defandExample;
}
xmlhttp.open("GET", "everythingtext.txt", true); // getting the actual .txt file to be used
xmlhttp.send();
</script>
<link rel="stylesheet" type="text/css" href="css/everythinghtmldynamize.css">
<link href="https://fonts.googleapis.com/css?family=Abril+Fatface|Raleway" rel="stylesheet">
</head>
<body>
<div id="menu"> yo </div>
<div id="defandexample"></div>
</body>
</html>
通过声明“xmlhttp”变量后立即声明变量“fields”和“oneLine”全局范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.