2009-10-15 92 views
0

我是JavaScript新手,所以請原諒我缺乏實際的代碼。爲每行添加偏移量

我有一塊文字,我想適合一個空間。我想把它分成幾行。我需要找到換行符在哪裏添加一個新行和一些偏移量。然後我想從行字長度中減去偏移量(如果需要的話),並獲得一個新的循環,直到有什麼要做。

我實際上希望得到一個關於在換行符上分割的答案,或者至少通過像素來查找一行是多久。

+0

我很抱歉,但我真的不明白你的問題 – Ikke 2009-10-15 11:53:39

+0

我需要基本上砍一些lenght,寬度和高度爲行的文本塊。 然後添加到增加縮進(padding-left)的每一行並將溢出的單詞裁剪到下一個新行,直到沒有剩下單詞並且一切都很好地傾斜爲止。 – Martin 2009-10-15 12:07:20

回答

1

爲什麼不使用元素偏移?

<div id="somediv" style="height:20px;width:100px;"> 
some content that wrap and makes new line some content that wrap and makes new line some content that wrap and makes new line some content that wrap and makes new line 
</div> 

<script> 
    var d=document.getElementById("somediv"); 
    alert(d.style.height);//outputs 20px 
    alert(d.offsetHeight);//outputs the actual/rendered height used by the element 
</script> 
0

據我所知,檢查某些文本的寬度的唯一方法是在隱藏的元素中呈現該文本,然後檢查該元素的寬度。繼續這樣做直到你得到的最長的字符串小於你想要的最大寬度。沖洗並重復。

我假設你正在試圖將你的文本調整爲任意/不規則形狀:如果它只是一個矩形,那麼當然,你可以設置一個容器元素的寬度。

0

爲了得到一個排的寬度可以使用

width()

功能

,並獲得高度,您可以使用

height()功能

<script> 
$(document).ready (function() { 
    alert ($("#tr1").width()); 
    alert ($("#tr1").height()); 
}); 

</script> 
<table> 
<tr id="tr1"> 
<td>test test testtest test testtest test testtest test test 
</td> 
<td>test test testtest test testtest test testtest test test 
</td> 
<td>test test testtest test testtest test testtest test test 
</td> 
</tr> 
</table>