简体   繁体   中英

Set initial value of a Span element

I have the following code to count number of paragraphs. How can I set the initial value to 0 rather than 1

JS

function counter(field) {
    var number = 0;
    var text = $(field).val();
    var word = $(field).val().split(/[ \n\r]/);
    var lines = $(field).val().split(/\r\n|\r|\n/g);
    words = word.filter(function(word) {
        return word.length > 0 && word.match(/^[A-Za-z0-9]/);
    }).length;
        $('.paraCount').text(lines.length);
}

See Fiddle

Thanks

Unfortunately, it's a special case. I think one condition is the best you can do, unless empty lines shouldn't be counted at all:

var lineCount = field.value ? field.value.split(/\r\n|[\r\n]/).length : 0;

$('.paraCount').text(lineCount);
$('.paraCount').text(lines.length-1);

http://jsfiddle.net/6B9Ga/10/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM