2011-10-11 76 views
0

我无法从HTML中创建一个影响,该影响包含一行居中文本溢出行的宽度。下面是我尝试过的一个例子,我得到了隐藏文本,但只在行的右侧。有人可以让我知道这是可能的吗?在HTML表格中隐藏溢出居中的文本

感谢,

詹姆斯

<div id="div1" style="display:inline; position: absolute; top:108px; left: 28px; width:403px; height:74px; background-color:transparent; z-index: 2; border: solid 0px #000000; color: #000000; "> 
     <table width="100%" height="100%" cellpadding="0" cellspacing="0" style="color: #000000; font-family: Microsoft Sans Serif; font-size: 8.25pt; table-layout:fixed; "> 
      <tr> 
       <td id="tsp1" valign="middle" nowrap="nowrap"><div style="height:100%; text-align:center; overflow:hidden;">This is a test of the text subpanel wrapping capabilities or lack there of in the thick client versus the web client.</div></td> 
      </tr> 
     </table> 
    </div> 

回答

0

这是一个“脏”方法来做到这一点:

<html> 
<head> 
<title>Your Example</title> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> 
<script type="text/javascript"> 
    <!-- 
    function setCentered() 
    { 
     currString=$("#target").text(); 
     if(currString.length>16) 
     { 
      //magic equation 
      amount=(currString.length-16); 
      $("#target").css("margin-left","-"+amount.toString()+"px"); 
     } 
    } 
    // --> 
</script> 
<head> 
<body onLoad="javascript:setCentered()"> 
    <div id="div1" style=" display:inline; position: absolute; top:108px; left: 28px; width:80px; height:74px; background-color:transparent; z-index: 2; border: solid 2px red; color: #000000; "> 
     <table width="100%" height="100%" cellpadding="0" cellspacing="0" style="color: #000000; font-family: Microsoft Sans Serif; font-size: 8.25pt; table-layout:fixed; "> 
      <tr> 
       <td id="tsp1" valign="middle" nowrap="nowrap" style="text-align:center; overflow:hidden;"><p id="target">Space test #122 of totalling.</p></td> 
      </tr> 
     </table> 
    </div> 
</body> 
</html> 

请记住,你必须提高魔术方程式js-code考虑了各种字体和dis播放设置。

+0

谢谢吉诺,现在存在的问题是我需要有这项工作的能力,左,右和中心的理由。另外,我可以有一个或多个表格,其中会包含不同的段落标记ID,因此必须考虑JavaScript。有任何想法吗?谢谢 – James

+0

1.关于理由,在等式中对它们进行管理:例如,您可以做一个开关案例,以不同的方式表现每种不同的理由。在正确的情况下,不要输入if,除非文本已经到达td-tag的左侧。 –

+0

2.关于表ID,在jQuery中,您可以使用each()函数遍历整个页面的每个表,而不管id标签如何。另外,您可以通过jQuery添加一个特殊的class-tag到页面的每个td元素(运行时,jQuery),并用它来识别js代码中的td标签。 (.addClass()) –