div内的中英文字符串换行问题

用HTML加javascript实现打字机样的效果…当字符串为英文时无法自动换行…中文时可以自动换行

请输入图片描述

 

<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title> test typing </title>

    <script type="text/javascript" src="js/temp.js">

 

    </script>

</head>

 

<body>

    <div id="myDiv" style="width:200px; height:100px; border:2px #ccc dashed; padding:10px"></div>

    <script>

        var typingText = "abcdefghijklmnopqrstuvwxyzwhy中文字符可以自动换行不会超过边框";

        var count = 0;

        var myBlock = document.getElementById("myDiv");

 

        function type(){

            if(count <= typingText.length){

            myBlock.innerHTML = typingText.substring(0, count);

            count++;

        }else{

            window.clearInterval(intervalID);

            }

        }

        var intervalID = window.setInterval(type, 200);

    </script>

</body>

</html>

 

原因:
对于长英文或数字,浏览器认为一个单词,html默认是不会回车换行的,所以没有将其换行处理。可以通过css样式来实现换行的效果 
css code: word-wrap:break-word;overflow:hidden;

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<style type="text/css">
* {
word-wrap: break-word;
}
</style>
</head>
<body>
<table width="100%" border="0" cellpadding="3" cellspacing="1" style="border:1px solid #CEDFF5;" bgcolor="#DFE8F6">
<tr bgcolor="#FFFFFF">
    <td align="center" nowrap width="300">
        <table width="100%" border="0" cellpadding="0" cellspacing="0" style="word-break:break-word,white-space: -moz-pre-wrap;table-layout:fixed;">
            <tr><td>BEHxyJHptKZ4GsvQe2oeyX_lNGwlMNmRENPvGs2qiO_Ej7yOj7VBPB8AmXzgyGB4be测试测试测试测试</td></tr>
        </table>
    </td>
</tr>
</table>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值