我有一个基本上是文本区域的评论框。我将 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"> </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"> </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);
});
};