echo " <script language='javascript'>toBreakWord(4, 'content2'); </script>"; 
放在while循环之后.

解决方案 »

  1.   

    这样的只有一行的值符合要求,其余的仍然不变:您看是一下这个样子的吗?<?php}echo "<script language='javascript'>toBreakWord(4, 'content2');</script>";echo "</table>"; 
    ?>
      

  2.   

    document.getElementById 只能取得一个元素, 得用document.getElementsByName<script type="text/javascript">
    function toBreakWord(intLen, id){
    var obj=id;
    //alert(obj);
    var strContent=obj.innerHTML;
    //alert(strContent)
    var strTemp="";
    while(strContent.length>intLen){
    strTemp+=strContent.substr(0,intLen)+"<br>"; 
    strContent=strContent.substr(intLen,strContent.length); 
    }
    //alert(strTemp);
    //alert(strContent);
    strTemp+= strContent;
    obj.innerHTML=strTemp;
    }
    window.onload=function (what){
    if(arguments.length   ==   0)  
              what='content2';  
         else  
              what=arguments[0];  
         
    obj=document.getElementsByName(what);
    len=obj.length;
    for (i=0;i<len;i++){
    toBreakWord(4,obj[i]);

    }
    }
    </script>
    <body>
    <table border=1>
    <tr>
    <td name="content2" id="content2">01234567890123456789</td>
    <td name="content2" id="content2">wertyuioplkjhghjklzx</td>
    </tr>
    <tr>
    <td name="content2" id="content2">01234567890123456789</td>
    <td name="content2" id="content2">wertyuioplkjhghjklzx</td>
    </tr>
    </table>
    </body>
      

  3.   

    首先感谢您的回复!按照您说的,我复制到本地测试一下,好像不行!结果应该是如下这样的:0123 | wert
    4567 | yuio
    8901 | plkj
    2345 | hghj
    6789 | klzx0123 | wert
    4567 | yuio
    8901 | plkj
    2345 | hghj
    6789 | klzxtoBreakWord(4,obj[i]);//没有起作用不知道哪里还得改一下,才行! 谢谢
      

  4.   


    <script language='javascript'>
    function toBreakWord(intLen, obj)
    {
    //var obj=document.getElementById(id);
    var strContent=obj.innerHTML; 
    var strTemp="";
    alert('1');
    while(strContent.length > intLen)
    {
    strTemp+=strContent.substr(0,intLen)+"<br>"; 
    strContent=strContent.substr(intLen,strContent.length); 
    }
    strTemp+= strContent;
    obj.innerHTML=strTemp;
    }
    window.onload = function()
    {
      var divObj = document.getElementsByTagName('td');
      for(var i=0; i< divObj.length; i++)
      {
        //alert(divObj[i]);
       if(divObj[i].id == "content2")
       {
       toBreakWord(3, divObj[i]);
       }
      }
    }
    </script><table border='1' id="divTable">
    <tr>
    <td name="content2" id="content2">01234567890123456789</td>
    <td name="content2" id="content2">wertyuioplkjhghjklzx</td>
    </tr>
    <tr>
    <td name="content2" id="content2">01234567890123456789</td>
    <td name="content2" id="content2">wertyuioplkjhghjklzx</td>
    </tr>
      <tr>
    <td name="content3" id="content3">01234567890123456789</td>
    <td name="content3" id="content3">wertyuioplkjhghjklzx</td>
    </tr>
    </table>注意第三个<tr>里面的没有改.
      

  5.   

    <script type="text/javascript">
    function toBreakWord(intLen, id){
    var obj=id;
    var strContent=obj.innerHTML;
    var strTemp="";
    while(strContent.length>intLen){
    strTemp+=strContent.substr(0,intLen)+"<br>"; 
    strContent=strContent.substr(intLen,strContent.length); 
    }
    strTemp+= strContent;
    obj.innerHTML=strTemp;
    }
    window.onload=function (){
        var obj=document.getElementsByName('content2');
        var len=obj.length;
        for (i=0;i<len;i++){
         toBreakWord(4,obj[i]);
        }
    }
    </script>
    <body>
    <table border=1>
    <tr>
    <td name="content2" id="content2">01234567890123456789</td>
    <td name="content2" id="content2">wertyuioplkjhghjklzx</td>
    </tr>
    <tr>
    <td name="content2" id="content2">01234567890123456789</td>
    <td name="content2" id="content2">wertyuioplkjhghjklzx</td>
    </tr>
    </table>
    </body>
      

  6.   

    <script type="text/javascript">
    function toBreakWord(intLen, id){
    var obj=document.getElementById(id);
    var strContent=obj.innerHTML; 
    var strTemp="";
    while(strContent.length>intLen){
    strTemp+=strContent.substr(0,intLen)+"<br>"; 
    strContent=strContent.substr(intLen,strContent.length); 
    }
    strTemp+= strContent;
    obj.innerHTML=strTemp;
    }
    </script>
    <!--
    <div id="content2" >hello world</div>
    <script language='javascript'>toBreakWord(4, 'content2');</script>
    --><?php
    $arr = array("0123456789","1234567890","2345678901");
    $array = array("helloworld","abcdefghigklmn","opqrstuvwxyz");foreach($arr as $key=>$value){
    echo "<div id='content".$key."'>$value</div>";
    echo "<script language='javascript'>toBreakWord(4, 'content".$key."');</script>";
    echo "<hr>";
    }foreach($array as $key=>$value){
    echo "<div id='cont".$key."'>$value</div>";
    echo "<script language='javascript'>toBreakWord(4, 'cont".$key."');</script>";
    echo "<hr>";
    }?>