简体   繁体   中英

Need help in my jquery plugin

Last week a made a function for ellipsing the text inside some selector.

I was calling the function like this:

ellipsiText('.class',50) passing the selector and the max length of the text that i wanted to. This works fine, but im trying to make it a plugin, to call like this: $('.class').ellipsiText(50).

So, i was reading the tutorial in jquery website, and i understood how to do it. But i think i'm having an issue with the "this" seletor. Here is my original function:

function ellipsiText(selector,maxLength){


    var array = $(selector).map(function(){
        return $(this).text();
    }).get();

    var i;

    var teste = [];

    for (i=0;i<array.length;i++){

        if (array[i].length > maxLength){

        teste.push(array[i].substr(0,maxLength) + "...");

        } else {

            teste.push(array[i]);
        }
    }

    for (var i=0;i<teste.length;i++){

    $(selector).each(function(i){

        $(this).text(teste[i]);

    });

    }


}

and here is my tentative of making a jquery plugin:

(function ($) {

    $.fn.ellipsiText = function(length){

        var array = $(this).map(function(){
            return $(this).text();
            }).get();

        var i;
        var teste = [];

        for (i = 0; i < array.length; i++){
            if (array[i] > length){
                teste.push(array[i].substr(0,length) + "...");
            } else {
                teste.push(array[i]);
            }
        }

        $(this).each(function(i){

            $(this).text(teste[i]);

        }); 
    };

}(jQuery));

What am i doing wrong?

Well first thing is not a problem, but instead of $(this) in the first function scope, you can use this.map/this.each .

The problem is, in the second code you do

if (array[i] > length)

instead of

if (array[i].length > length)

Nothing to do with the jQuery plugin!

http://jsfiddle.net/UY88r/

You already have a working function, just use it.

$.ellipsiText = ellipsiText;
$.fn.ellipsiText = function (count) {
    ellipsiText(this, count);
}

now you can use it like any of the following:

ellipsiText('.class',50);
$.ellipsiText('.class',50);
$('.class').ellipsiText(50);

There's no sense in rewriting the function you already have working when you can just use it.

This is untested, but the basic structure is something like this. Also you have so much looping in your code when one loop is needed.

$.fn.ellipsiText= function(options) {

    var settings = $.extend({  //nice way to give to give defaults
        length : 50,
        ellipsi : "..."
        }, options );

    return this.each(function() {  //the return is needed for chaining
        var elem = $(this);
        var txt = elem.text();
        if (txt.length>settings.length) {
             elem.text(txt.substr(0,settings.length) + settings.ellipsi );
        }
    });

};

and call it

$( "div.defaults" ).ellipsiText();

$( "div.foo" ).ellipsiText({
    length : 10
});

$( "div.more" ).ellipsiText({
    length : 10,
    ellipsi : "<more>"    
});

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