2

我正在开发一个优惠券应用程序,它有一个区域,企业主可以在其中输入一个简短的“标签行”来说明优惠券的用途。例如:

  • 一份主菜优惠10%!
  • 减 5 美元
  • 晚上 7 点前 2 换 1 清酒
  • 如果您的名字是 Gary,则可获得 15 个免费烙饼

同样,这是用户生成的。它将显示在客户可以看到的屏幕上。我希望它在一定数量的像素内显示在一行上。它将在 Arial 中。

考虑到很多浏览器,确定允许它们包含的最大字符数的最佳方法是什么,这样它就不会占用额外的行或溢出......?

4

1 回答 1

0

If you know the font, font-size, and total width of the one line then you could just do a trial and error calculation (that is, enter characters until the end of the line).

Once you know that, you can check for the total # of characters on keyup and alert if that amount is reached.

For instance, as an example only, let's say you choose Arial 14px and you've tested it to see that 10 characters is about all that one line can hold. Then you can add something like this

$('textarea').keyup(function(){
    if($(this).val().length == 10){
        alert('You\'ve reached the limit');
    }
});

Working Example: http://jsfiddle.net/uGDKn/

于 2011-10-12T03:24:55.330 回答