动态地将选项元素添加到电子中的<select>

时间:2016-09-15 06:58:17

标签: javascript html electron

请理解这个问题与电子中的jscript有关。 我有两个.js文件要打开并读入单个数组。我把它设置为主类型函数中的两个函数。 每个都在JavaScript中的html文件中调用。它们用于填充或向选择输入框添加选择选项。 我很难调用它们(数组),因此可以将其读入选择选项代码。下面是(&#39; ./ app.js&#39;) var zipArry = []; var zipResult = []; var aniArry = []; var aniResult = []; var blah =(function(){    function zipList(callback){     var lineReader = require(&#39; readline&#39;)。createInterface({     输入:require(&#39; fs&#39;)。createReadStream(&#39; .Text1.txt&#39;)     });     lineReader.on(&#39; line&#39;,function(line){         zipArry.push(线);     });     lineReader.on(&#39; close&#39;,function(line){     var Ncount = 0;     for(var x = 0; x&lt; zipArry.length; x ++){         zipResult.push(zipArry [X]);     }   });     回调(zipResult); }  function aniList(callback){     var lineReader = require(&#39; readline&#39;)。createInterface({     输入:require(&#39; fs&#39;)。createReadStream(&#39; ./ text2.txt&#39;)     });     lineReader.on(&#39; line&#39;,function(line){        aniArry.push(线);     });     lineReader.on(&#39; close&#39;,function(line){     var Ncount = 0;     for(var x = 0; x&lt; aniArry.length; x ++){          aniResult.push(aniArry [X]);         }   });    回调(aniResult);  }    返回{       ziplist:zipList,       anilist:aniList    }; });    module.exports = blah; 这是带有JavaScript代码的.html文件的一部分。如果有办法让这更容易请解释。我是个菜鸟。 var blah = require(&#39; ./ apps2.js&#39;); var Blah = new blah(); Blah.ziplist(); var select = document.getElementById(&#34; zip&#34;); for(var i = 0; i&lt; zipResult.length; i ++){   var el = document.createElement(&#39; option&#39;);       var opt = zipResult [i];       el.textContent = opt;       el.value = opt;       select.appendChild(EL); }

1 个答案:

答案 0 :(得分:0)

这是答案。我改变了东西并制作了对象,然后将它们导出到html文件中。

var zipArry = []; 
var zipResult = [];
var aniArry = [];
var aniResult = [];

var blah = {

   ziplist: function zipList(callback) {


       var lineReader = require('readline').createInterface({
          input: require('fs').createReadStream('.Text1.txt')
       });

       lineReader.on('line', function (line) {
           zipArry.push(line);
       });

       lineReader.on('close', function (line) {

          var Ncount = 0;
          for(var x = 0; x < zipArry.length; x++){

             zipResult.push(zipArry[x]);

          }
          callback(zipResult);
       });

   }



   anilist: function aniList(callback) {


       var lineReader = require('readline').createInterface({
          input: require('fs').createReadStream('./text2.txt')
       });

       lineReader.on('line', function (line) {
           aniArry.push(line);
       });

       lineReader.on('close', function (line) {

           var Ncount = 0;
           for(var x = 0; x < aniArry.length; x++){

              aniResult.push(aniArry[x]);
           }
           callback(aniResult);
       });

    }

 }; 

   module.exports = blah;

在html文件中

var blah = require('./apps2.js');

Blah.ziplist(function(zipResult){

    var select = document.getElementById("zip");  
    for (var i = 0; i < zipResult.length; i++){

        var el = document.createElement('option');
        var opt = zipResult[i];
        el.textContent = opt;
        el.value = opt;
        select.appendChild(el);
    }
});

Blah.anilist(function(aniResult){

   var select = document.getElementById("ani");  
   for (var i = 0; i < aniResult.length; i++){

       var el = document.createElement('option');
       var opt = aniResult[i];
       el.textContent = opt;
       el.value = opt;
       select.appendChild(el);
   }
});