作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在阅读这个文本文件...我将其读入数组。
fields[0]
是术语 fields[1]
是定义和 fields[2]
就是这个例子。
我试图将从文本文件中获取的数组传递到另一个函数中,以便我可以在另一个函数中显示定义和示例 <div>
。这是我的代码:
<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”,使它们成为全局作用域。
关于javascript - 如何将数组获取到另一个函数,以便将其显示到另一个 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42463908/
我是一名优秀的程序员,十分优秀!