2

我在互联网上找到了一个 textarea 插件,并对其进行了一些修改以满足我的需要。它以我想要的方式工作,除了一个小细节。

如您所见,当我键入或删除字符时,它会展开和折叠文本区域。

如果我按下回车键,它会生成一个评论 div,其中包含我在 textarea 中输入的内容。

我的问题是,**如何在<br />每次用户按下 Shift + Enter 时将一个附加到我的 new_comment 变量?

/*!
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <jevin9@gmail.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return. Jevin O. Sewaruth
 * ----------------------------------------------------------------------------
 *
 * Autogrow Textarea Plugin Version v2.0
 * http://www.technoreply.com/autogrow-textarea-plugin-version-2-0
 *
 * Date: March 13, 2011
 */
jQuery.fn.autoGrow = function(){

    return this.each(function(){
        // Variables
        var colsDefault = this.cols;
        var rowsDefault = this.rows;

        //Functions

        var grow = function() {
            growByRef(this);
        }



        var onFocus = function(obj) {

      if ($(this).val() != 'Write a comment...') {
        return;
      } else {
        $(this).parents(".comment_new_container").children("img").show();
        //$(this).height(34);
        $(this).width(745);
        $(this).val(''); 
      }

        }

        var onBlur = function(obj) {

      if ($(this).val().length == 0) { 
        $(this).parents(".comment_new_container").children("img").hide();
        //$(this).height(16);
        $(this).width(795);
        $(this).val('Write a comment...');
      } 

        }


        var growByRef = function(obj) {

        var new_comment = '';

      if (!obj.shiftKey && obj.keyCode == 13) {

        obj.preventDefault();

        new_comment += '<div class="comment_container" id="001">';
        new_comment += '<a href="#"><i class="comment_delete">&nbsp;</i></a>';
        new_comment += '<img src="img/avatar45.png" />';
        new_comment += '<a href="/">Mickey Mouse</a>';
        new_comment += '<br/>';
        new_comment += '<div class="comment_content">' + $(this).val(); + '</div> <!-- End comment_content -->';
        new_comment += '<div class="comment_timestamp">13 minutes ago</div> <!-- End comment_timestamp -->';
        new_comment += '</div> <!-- End comment_container -->';

        $(new_comment).insertBefore($(this).parents(".comment_new_container"));

            var comment_total = parseInt($('.social_menu_right li a').children('.meta.comment_total').text(), 10) + 1;
          $('.social_menu_right li a').children('.meta.comment_total').text(comment_total);


        $(this).val('Write a comment...');
        $(this).blur();
        growByRef(this);



      } else {

        var linesCount = 0;
        var lines = obj.value.split('\n');

        for (var i=lines.length-1; i>=0; --i)
        {
          linesCount += Math.floor((lines[i].length / colsDefault) + 1);
        }

        if (linesCount >= rowsDefault) {
          obj.rows = linesCount + 1;
        } else {
          obj.rows = rowsDefault;           
        }

            }

    }

        var characterWidth = function (obj){
            var characterWidth = 0;
            var temp1 = 0;
            var temp2 = 0;
            var tempCols = obj.cols;

            obj.cols = 1;
            temp1 = obj.offsetWidth;
            obj.cols = 2;
            temp2 = obj.offsetWidth;
            characterWidth = temp2 - temp1;
            obj.cols = tempCols;

            return characterWidth;
        }

        // Manipulations

        $(this).css("width","auto");
        $(this).css("height","auto");
        $(this).css("overflow","hidden");

        this.style.width = ((characterWidth(this) * this.cols) + 6) + "px";

        $(this).bind("focus", onFocus);
        $(this).bind("blur", onBlur);
        $(this).bind("keypress", growByRef);
        $(this).bind("keyup", grow);

    });
};

感谢你们。

4

2 回答 2

1

尝试在 growByRef 函数的顶部添加它:

  if (obj.shiftKey && obj.keyCode == 13) {
    $(this).val($(this).val() + "\n");
    return;
  }
于 2012-07-20T15:15:31.923 回答
-1

在此处 查看这两个链接

此案例与您的相似

于 2012-07-20T15:12:03.423 回答