2

我有一个基本上是文本区域的评论框。我将 cols 属性设置为 113 cols,它在 Firefox 中看起来不错,但在 chrome 或 IE9 中却不行。

然后我将字体更改为固定宽度的字体,所有 3 个浏览器都正确显示它。关于它的事情是,它只是丑陋的。

您对为什么它只能正确计算 Firefox 中列的宽度而不是其他列的宽度有任何想法吗?

谢谢你。

这是CSS:

#wrap_mid #nav_body .entry_container .comment_new_container .ta_new_comment
{
    border: 1px solid #d5d5d5;
    border-radius: 3px;
    color: #999;
    font-family: Arial, Helvetica, sans-serif;
    font-size: small;
    padding: 3px 4px;
    width: 795px;
    resize: none;

}

这是HTML:

<div class="comment_new_container">
<img src="img/avatar45.png" />
<textarea class="ta_new_comment" cols="113" rows="0">Write a comment...</textarea>
<!-- Float fix --> 
<div class="clear">&nbsp;</div>
</div> <!-- END comment_new_container -->

这是JQuery:

<script type="text/javascript">

$(function(){

  $(".ta_new_comment").autoGrow();

});

</script>

这是 JQuery 插件:

/*!
 * ----------------------------------------------------------------------------
 * "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().replace(/\n/g, '<br />'); + '</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","795");
        $(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

5

使用 CSS 并定义文本区域的宽度/高度

取出 cols 和 rows 标签并使用 CSS

于 2012-07-20T19:33:41.867 回答
3

顾名思义,等宽字体对于每个字母、数字等具有相同的宽度(按字符)。这意味着 113 列总是相同的长度,而大多数字体的“M”比“I”宽使文本的整体宽度变化。

尝试使用 CSSwidth属性指定宽度。此外,不要忘记您在 Internet 上找到的几乎所有输入字段(例如此处)都是等宽字体。

于 2012-07-20T19:38:22.880 回答