2011-03-11 36 views
1

我使用的jQuery(http://wplayground.comuv.com/counter/)反插件,有它在我们的页面成功实施。但我需要对它进行调整,我不确定它应该去哪里。调整jQuery的字计数器计数网址为3个字

基本上,脚本统计字符数(或词,在我们的情况下),当用户已经达到了目标停止输入。这部分工作正常。但现在,我需要进行调整,这样,如果用户输入如example.com一个URL,这个词被计为3个字,而不是1.在我们的网站的前世,开发商写自定义JavaScript来完成任务,并考虑到URL的事情,他做了以下更改:

if(getWord.indexOf(".com")>=1 || getWord.indexOf(".net")>=1 || getWord.indexOf(".org")>=1 || getWord.indexOf(".biz")>=1 || getWord.indexOf(".gov")>=1 || getWord.indexOf(".edu")>=1 || getWord.indexOf(".mobi")>=1 || getWord.indexOf(".info")>=1 || getWord.indexOf(".ca")>=1 || getWord.indexOf(".us")>=1 || getWord.indexOf(".mil")>=1){///Email 
    pk1 = pk1+3; 

因此,以下是新的jquery计数器。我如何调整它来做同样的事情?

/* 
    jQuery (character and word) counter 
    Copyright (C) 2009 Wilkins Fernandez 

    This program is free software: you can redistribute it and/or modify 
    it under the terms of the GNU General Public License as published by 
    the Free Software Foundation, either version 3 of the License, or 
    (at your option) any later version. 

    This program is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
    GNU General Public License for more details. 

    You should have received a copy of the GNU General Public License 
    along with this program. If not, see <http://www.gnu.org/licenses/>. 
*/ 
(function($) { 
    $.fn.extend({ 
     counter: function(options) { 
      // Set the default values, use comma to separate the settings 
      var defaults = { 
       type: 'char', // {char || word} 
       count: 'down', // count {up || down} from or to the goal number 
       goal: 140  // count {to || from} this number     
      }; 
      var options = $.extend({}, defaults, options); 
      var flag = false; // Set to true when goal is reached 

      // Loop through each instance of the object that being is passed (the users selector). 
      // Allows for multiple instances of the jQuery methods available to THIS (instance of the) object 
      // (you can use this plug-in more than once on a page). 
      return this.each(function() { 
       var msg; 
       var $obj = $(this); 

       // Sets the appropriate message based on the options 
       function get_msg_equation(objLength) { 
        // Make sure that the right values are set 
        if (typeof options.type !== 'string') { 
        } else { 
         switch (options.type) { 
          case 'char': 
           if (options.count === 'down') { 
            msg = " character(s) left"; 
            return (options.goal - objLength); 
           } 
           else if (options.count === 'up') { 
            msg = " characters (" + options.goal + " max)"; 
            return objLength; 
           } 
           break; 
          case 'word': 
           if (options.count === 'down') { 
            msg = " word(s) left"; 
            return (options.goal - objLength); 
           } 
           else if (options.count === 'up') { 
            msg = " words (" + options.goal + " max)"; 
            return objLength; 
           } 
           break; 
          default: 
         } //END switch 
        } // END if 
       } // END function 

       // * Initialize *: the bind event needs an object to bind to 
       $('<div id=\"' + this.id + '_counter\"><span>' + get_msg_equation($($obj).val().length) + '</span>' + msg + '</div>').insertAfter($obj); 

       // Cache the counter selector 
       var $currentCount = $("#" + this.id + "_counter" + " span"); 

       // Bind events to a function that returns the length 
       // of the characters || words in the given text field. 
       $obj.bind('keyup click blur focus change paste', function(new_length) { 
        // Update characters depending on the option selected 
        switch (options.type) { 
         case 'char': 
          new_length = $($obj).val().length; 
          break; 
         case 'word': 
          if ($obj.val() === '') { 
           new_length = 0; 
          } 
          else { 
           new_length = $.trim($obj.val()) 
            .replace(/\s+/g, " ") 
            .split(' ').length; 
          } 
          break; 
         default: 
        } // END switch 

        // Set flag TRUE when counter reaches goal 
        switch (options.count) { 
         case 'up': 
          if (get_msg_equation(new_length) >= options.goal && options.type === 'char') { 
           $(this).val($(this).val().substring(0, options.goal)); 
           flag = true; 
           break; 
          } 
          if (get_msg_equation(new_length) === options.goal && options.type === 'word') { 
           flag = true; 
           break; 
          } else if (get_msg_equation(new_length) > options.goal && options.type === 'word') { 
           $(this).val(""); 
           $currentCount.text("0"); 
           flag = true; 
           break; 
          } 
          break; 
         case 'down': 
          if (get_msg_equation(new_length) <= 0 && options.type === 'char') { 
           $(this).val($(this).val().substring(0, options.goal)); 
           flag = true; 
           break; 
          } 
          if (get_msg_equation(new_length) === 0 && options.type === 'word') { 
           flag = true; 
          } else if (get_msg_equation(new_length) < 0 && options.type === 'word') { 
           $(this).val(""); 
           flag = true; 
           break; 
          } 
          break; 
         default: 
        } // END switch 

        // Listen on keydown to catch the last character or word typed 
        // and prevent the user from typing 
        $obj.keydown(function(event) { 
         if (flag) { 
          this.focus(); 
          // Listen for delete & backspace 
          if ((event.keyCode !== 46 && event.keyCode !== 8)) { 
           if ($(this).val().length > options.goal && options.type === 'char') { 
            $(this).val($(this).val().substring(0, options.goal)); 
            return false; // Stop the default action (typing) 
            // Listen for blank (spacebar) & return 
           } else if (event.keyCode !== 32 && event.keyCode !== 8 && options.type === 'word') { //Allow to continue typing last word 
            return true; 
           } else { 
            return false; // Stop the default action (typing) 
           } 
          } else { 
           flag = false; 
           return true; 
          } 
         } 
        }); // END keydown 
        $currentCount.text(get_msg_equation(new_length)); 
       }); // END Bind 
      }); //END return 
     } // END counter function 
    }); // END extend 
}) // END function 
(jQuery); // Return jQuery object 
+0

为什么'example.com'是3个字? – 2011-03-11 15:25:13

+1

如果你真的必须知道,这是因为该公司必须每个字的用户收费。公司也支付每个字。当公司向他们的出版商付款时,出版商将URL记为3个字。因此,当允许用户输入他们的文本时,我们必须将URL计为3个字。简单。 – Kevin 2011-03-11 15:30:56

回答

3

在柜台插件文件的中间,有一条线,看起来像这样:

else{ 
    new_length = $.trim($obj.val()) 
    .replace(/\s+/g, " ") 
    .split(' ').length; 
} 

好像这是重要的地方的话。它抓住框中的文本,分割空间,然后计算元素。

你可以添加某种检查(正则表达式),这里的URL,然后递增new_length相应。

像这样(未经):

else{ 
    var str = $.trim($obj.val()) 
    .replace(/\s+/g, " "); 
    new_length = str.split(' ').length; 
    var URLs = str 
    .match(/([^\s])+\.(com|net|org|biz|gov|edu|mobi|info|ca|us|mil)/g); 
    if(URLs != null && URLs.length > 0){ 
    new_length += (URLs.length*2); 
    } 
} 
+0

感谢您的帮助。我已经找到了这段代码,但不能让它做我想做的事(我对jquery和JS是一个完全新手)。我实现了你的修复,但是在firebug中得到了以下内容:“无法读取null的属性'length' – Kevin 2011-03-11 16:00:18

+0

@Kevin:是的,我根本没有测试过。让我解决这个问题。 – 2011-03-11 16:06:26

+0

@Kevin:尝试在回答新的代码。我修好了它。 – 2011-03-11 16:06:50