【问题标题】:Count # of non-blank lines in a text file with Javascript?使用Javascript计算文本文件中非空行的数量?
【发布时间】:2013-09-02 21:41:26
【问题描述】:

您好,我正在尝试使用 JS 或 Jquery 计算文本文件中的所有非空行。

现在我正在使用 2 步方法...但它仍在计算空白行。

我正在从输入框中提取文本文件,并在文本区域上方显示文件信息以及文本区域中的内容。

Evertyhing 似乎工作得很好,只是我无法让 linecount.js 跳过空白行……这在 jQuery 中是否可行?

JS将文本文件加载到textarea中:

   var reader; //GLOBAL File Reader object for demo purpose only

/**
 * Check for the various File API support.
 */
function checkFileAPI() {
    if (window.File && window.FileReader && window.FileList && window.Blob) {
        reader = new FileReader();
        return true; 
    } else {
        alert('The File APIs are not fully supported by your browser. Fallback required.');
        return false;
    }
}

/**
 * read text input
 */
function readText(filePath) {
    var output = ""; //placeholder for text output
    if(filePath.files && filePath.files[0]) {           
        reader.onload = function (e) {
            output = e.target.result;
            displayContents(output);
        };//end onload()
        reader.readAsText(filePath.files[0]);
    }//end if html5 filelist support
    else if(ActiveXObject && filePath) { //fallback to IE 6-8 support via ActiveX
        try {
            reader = new ActiveXObject("Scripting.FileSystemObject");
            var file = reader.OpenTextFile(filePath, 1); //ActiveX File Object
            output = file.ReadAll(); //text contents of file
            file.Close(); //close file "input stream"
            displayContents(output);
        } catch (e) {
            if (e.number == -2146827859) {
                alert('Unable to access local files due to browser security settings. ' + 
                 'To overcome this, go to Tools->Internet Options->Security->Custom Level. ' + 
                 'Find the setting for "Initialize and script ActiveX controls not marked as safe" and change it to "Enable" or "Prompt"'); 
            }
        }       
    }
    else { //this is where you could fallback to Java Applet, Flash or similar
        return false;
    }       
    return true;
}   

/**
 * display content using a basic HTML replacement
 */
function displayContents(txt) {
    var el = document.getElementById('countMe'); 
    el.innerHTML = txt; 

}  

小提琴的行数:

$(document).ready(function(){

var lines = 5;
var linesUsed = $('#linesUsed');

$('#countMe').keydown(function(e) {

    newLines = $(this).val().split("\n").length;
    linesUsed.text(newLines);

    if(e.keyCode == 13 && newLines >= lines) {
        linesUsed.css('color', 'red');
        return false;
    }
    else {
        linesUsed.css('color', '');
    }
});

});

输入代码和文本区:

    <input type="file" id="files" name="files[]" onchange='readText(this)' />
   <textarea name="countMe" cols="58" rows="18" id="ta"></textarea>
   <div class="theCount">Lines used: <span id="linesUsed">0</span><div>

【问题讨论】:

    标签: javascript jquery line-count


    【解决方案1】:

    您正在计算行数,但没有检查每行中是否有任何内容。将行数组保存在一个变量中并循环遍历这些行以检查其中是否有任何内容:

    newLines = 0;
    var lines = $(this).val().split("\n");
    for (var i = 0; i < lines.length; i++) {
      if (lines[i].length > 0) newLines++;
    }
    linesUsed.text(newLines);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 2011-03-29
      相关资源
      最近更新 更多