2017-02-26 6 views
1

私はこのテキストファイルを読んでいますが、配列に読み込んでいます。配列を別の関数に渡して別のdivに表示できるようにするには

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();"> ' 
           + '&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> 

答えて

0

あなたが「XMLHTTP」変数を宣言した直後にそれらを宣言することによって、変数「フィールド」と「ONELINE」グローバルスコープを作成します。

関連する問題