2

定義したTDの幅を知りたいstyle='width: 200px;のですが、テキストで長くなります(切れ目なし)。有効な幅を調べたいのですが、200ピクセルよりも大きい限り、その中のテキストサイズを毎回約2ピクセル縮小します。

これは可能ですか?

4

2 に答える 2

3

This can be done in javascript but it's a little complex.

Here's the complete working example : http://jsfiddle.net/dystroy/kdrrA/

The HTML :

<table><tr><td id=txtcell nowrap><span id=txt>My long text is long. My long text.</span></td></tr></table>
<br>Font Size : <span id=mes></span>​

The CSS :

#txtcell{ max-width:200px;}

The javascript :

var fontSize = 20; 
var reduce = function() { 
    while ($('#txt').width() > 200) { 
        fontSize -= 1; 
        $('#txt').css('font-size', fontSize); 
    }
    $('#mes').html(fontSize); // this line is only for demonstration, remove it in "production"
}; 
reduce();

Note that I had to add a span in the cell because TD width computation by jquery doesn't work very well.

于 2012-06-08T07:26:18.377 に答える
0

作るにはJavaScriptが必要です。ただし、CSS だけで作成したい場合は、CSS 式を使用できますexpression

td {width: expression(this.style.width);}

それが役に立てば幸い!:)

于 2012-06-08T07:10:42.410 に答える