国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

javascript - Le plug-in jQuery pour intercepter la longueur de la cha?ne rencontre le problème de ne pas pouvoir obtenir correctement le text() dans l'élément
typecho
typecho 2017-06-28 09:28:00
0
2
1041

J'ai créé un plug-in pour intercepter la longueur d'une cha?ne. Lorsque le nombre de caractères dépasse le nombre spécifié, les caractères sont interceptés et l'invite de contenu complet s'affiche en suivant la souris.
Cela peut être réalisé en écrivant la fonction seule, mais après avoir utilisé la méthode suivante pour créer un plug-in, j'ai trouvé que le contenu suivi par la souris est le dernier, et le contenu du dernier sera affiché indépendamment de si les caractères sont dépassés, car lorsqu'il y a une partie du contenu Il est chargé dynamiquement, donc la délégation d'événements est utilisée.

(function($, window, document, undefined) {
    // 創(chuàng)造一個(gè)公共變量來(lái)構(gòu)建一個(gè)私有方法
    var privateFunction = function() {}
    var methods = {
        // 字符截取,鼠標(biāo)觸發(fā)跟隨詳情提示框
        subString: function (options) {
            return this.each(function(index) {
                var $this = $(this);
                var defaults = {
                    el: '',      // 目標(biāo)元素
                    charNum: 22,    // 截取字符個(gè)數(shù)
                    hasDot: true,   // 是否顯示省略號(hào)
                    // dotColor: '#666'   // 省略號(hào)顏色
                    allTextp: '#allText',       // 鼠標(biāo)跟隨完整文本框的p
                    isPrompt: true      // 是否顯示提示框
                };
                var settings = $.extend({}, defaults, options),
                    allTextp = settings.allTextp.replace(/[#|.]/g, ''),
                    strText = $(settings.el).eq(index).text(),
                    chineseRegex = /[^\x00-\xff]/g,
                    strLength = strText.replace(chineseRegex, '**').length,
                    newLength = 0,
                    newStr = '',
                    singleChar = '';
                function _subString(str, len, hasDot) {
                    for (var i = 0; i < strLength; i++) {
                        singleChar = str.charAt(i).toString();
                        singleChar.match(chineseRegex) != null ? newLength += 2 : newLength++;
                        if (newLength > len) break;
                        newStr += singleChar;
                    }
                    if (hasDot && strLength > len) newStr += '...';
                    return newStr;
                }
                // 截取字符串
                $this.html(_subString(strText, settings.charNum, settings.hasDot));
                // 鼠標(biāo)跟隨是否顯示完整文本框
                if ( (strLength > settings.charNum) && settings.isPrompt ) {
                    $(document).on('mouseover', settings.el, function(event) {
                        if ( settings.allTextp.indexOf('#') != -1 ) $('body').append('<p id="' + allTextp + '" />');
                        if ( settings.allTextp.indexOf('.') != -1 ) $('body').append('<p class="' + allTextp + '" />');
                    });
                    $(document).on('mousemove', settings.el, function(event) {
                        $(settings.allTextp).text(strText).show().css({
                            left: event.pageX + 30,
                            top: event.pageY
                        });
                    });
                    $(document).on('mouseout', settings.el, function(event) {
                        $(settings.allTextp).remove();
                    });
                    // $this.mouseover(function() {
                    //     if ( settings.allTextp.indexOf('#') != -1 ) $('body').append('<p id="' + allTextp + '" />');
                    //     if ( settings.allTextp.indexOf('.') != -1 ) $('body').append('<p class="' + allTextp + '" />');
                    // });
                    // $this.mousemove(function(event) {
                    //     $(settings.allTextp).text(strText).show().css({
                    //         left: event.pageX + 30,
                    //         top: event.pageY
                    //     });
                    // });
                    // $this.mouseout(function() {
                    //     $(settings.allTextp).remove();
                    // });
                }
            });
        }
        };
    $.fn.inCommonUseJsPlugin = function() {
        var method = arguments[0];
        if(methods[method]) {
            method = methods[method];
            arguments = Array.prototype.slice.call(arguments, 1);
        } else/* if( typeof(method) == 'object' || !method ) {
            method = methods.init;
        } else */{
            $.error( 'Method ' +  method + ' does not exist on jQuery.pluginName' );
            return this;
        }
        return method.apply(this, arguments);
    }
})(jQuery, window, document);
typecho
typecho

Following the voice in heart.

répondre à tous(2)
世界只因有你

Mon cerveau était confus pendant les heures de travail. Je suis rentré chez moi et je l'ai réécrit. Ce serait beaucoup plus facile si je changeais de fa?on de penser.

/**
 * 
 * @authors xxy
 * @date    2017-06-26 19:19:42
 * @url http://www.hifrontend.com
 */

(function($, window, document, undefined) {
    var methods = {
        // 字符截取,鼠標(biāo)觸發(fā)跟隨詳情提示框
        subString: function (options) {
            var $this = $(this);
            var defaults = {
                el: 'li',      // 目標(biāo)元素
                charNum: 22,    // 截取字符個(gè)數(shù)
                hasDot: true,   // 是否顯示省略號(hào)
                // dotColor: '#666'   // 省略號(hào)顏色
                allTextp: '#allText',       // 鼠標(biāo)跟隨完整文本框的p
                isPrompt: true      // 是否顯示提示框
            };
            var settings = $.extend({}, defaults, options);
            function _subString(str, len, hasDot) {
                var newLength = 0;
                var newStr = "";
                var chineseRegex = /[^\x00-\xff]/g; // 提取中文漢字
                var singleChar = "";
                var strLength = str.replace(chineseRegex, "**").length; // 將中文替換成 ** 并計(jì)算長(zhǎng)度
                for (var i = 0; i < strLength; i++) {
                    singleChar = str.charAt(i).toString();
                    (singleChar.match(chineseRegex) != null) ? newLength += 2 : newLength++;
                    if (newLength > len) break;
                    newStr += singleChar;
                }
                if (hasDot && strLength > len) newStr += "...";
                return newStr;
            }
            $(settings.el).each(function() {
                var text = $(this).text();
                $(this).attr('data-text', text); 
                $(this).html(_subString(text, settings.charNum, settings.isPrompt));
            });
            // 鼠標(biāo)跟隨是否顯示完整文本框
            $(document).on('mouseover', settings.el, function() {
                var allTextLen = $(this).attr('data-text').replace(/[^\x00-\xff]/g, "**").length;
                if ( allTextLen > settings.charNum ) {
                    var allTextp = settings.allTextp.replace(/[#|.]/g, '')
                    if ( settings.allTextp.indexOf('#') != -1 ) $('body').append('<p id="' + allTextp + '" />');
                    if ( settings.allTextp.indexOf('.') != -1 ) $('body').append('<p class="' + allTextp + '" />');
                }
            });
            $(document).on('mousemove', settings.el, function(event) {
                $(settings.allTextp).text( $(this).attr('data-text') ).show().css({
                    left: event.pageX + 30,
                    top: event.pageY
                });
            });
            $(document).on('mouseout', settings.el, function() {
                $(settings.allTextp).remove()
            });
            return this;
        }
    };
    $.fn.inCommonUseJsPlugin = function() {
        var method = arguments[0];
        if(methods[method]) {
            method = methods[method];
            arguments = Array.prototype.slice.call(arguments, 1);
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.pluginName' );
            return this;
        }
        return method.apply(this, arguments);
    }
})(jQuery, window, document);
phpcn_u1582
var settings = $.extend({}, defaults, options),
    allTextp = settings.allTextp.replace(/[#|.]/g, ''),
    strText = $(settings.el).eq(index).text(),
    chineseRegex = /[^\x00-\xff]/g,
    strLength = strText.replace(chineseRegex, '**').length,
    newLength = 0,
    newStr = '',
    singleChar = '';

Lorsqu'il est écrit ainsi, allTextp est considéré comme local ou global ? On dit que certains navigateurs plus anciens le considéreront comme global. Dans ce cas, on peut expliquer que le contenu suivi par la souris est toujours le dernier. Du point de vue du code, je ne vois aucun autre problème pouvant provoquer ce phénomène.

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal