简体   繁体   English

在我的jquery插件中需要帮助

[英]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. ellipsiText('。class',50)传递选择器和我想要的文本的最大长度。 This works fine, but im trying to make it a plugin, to call like this: $('.class').ellipsiText(50). 这工作正常,但我试图使它成为一个插件,像这样调用:$('。class')。ellipsiText(50)。

So, i was reading the tutorial in jquery website, and i understood how to do it. 所以,我正在阅读jquery网站上的教程,我明白了该怎么做。 But i think i'm having an issue with the "this" seletor. 但我认为我对“这个”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: 这是我尝试制作一个jquery插件:

(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 . 首先,问题不是问题,但在第一个函数范围中,不是$(this) ,而是使用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! 与jQuery插件无关!

http://jsfiddle.net/UY88r/ 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>"    
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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