简体   繁体   English

如何将数组转换为另一个函数,以便将其显示到另一个div中

[英]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();"> '
                                 + '&lt;' 
                                 + fields[0]
                                 + '&gt;'
                                 + ' </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.

相关问题 如果另一个div中的值在Javascript中为空,如何使div不显示? - How do I get div not to display if value in another div is empty in Javascript? 如何封装jQuery get方法,以便可以将其称为另一个函数? - How can I encapsulate the jQuery get method so I can call it as another function? 单击另一个DIV时如何使DIV消失? - How do I get DIV to disappear when another DIV is clicked? 如何通过单击另一个 div 来获取 div 的 class? - How can i get the class of a div by clicking another div? 如何获取从数组调用的按钮的当前值,以显示另一个数组的内容? - How do I get the current value of a button being called from an array to display something from another array? 如何在另一个div中集中一个div? - How do I center a div in another div? 如何在 JavaScript 中的另一个 div 中打印月份的天数,以便我可以将其相对定位在这个新的 div 中? - How to print the month days in another div in JavaScript, so that I can relatively position it inside this new div? 在Jquery中,如何使div从A平稳滑到B,以便另一个div在A中消失? - In Jquery, how can I make a div smoothly slide down from A to B so that another div fades in A? 如何将函数返回的数组复制到 JavaScript 中的另一个数组中? - How do I copy an array returned by a function into another array in JavaScript? 我如何做数组来分配另一个数组并修剪? - How can I do array to assign another array and trim?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM