Tips:点此可运行HTML源码

JS 英文不截断单词截取

canRun

View Code
<html>
<head>
    <title>JS substr</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Language" content="zh-CN" />
</head>
<body>
</body>
<div>原文:I want this effect, not that one.</div>
<div id="substring"></div>
<script type="text/javascript">
    function customSubstr(str,br){
        if(br<str.length&&str.charAt(br) != ' ') br=str.indexOf(' ',br);  //判断当前字符非空格,则查找向后第一个空格
        return str.substr(0,br);
    }
    var str = "I want this effect, not that one.";
    document.getElementById('substring').innerHTML = '截取22位:'+customSubstr(str,22);
</script>
</html>
posted @ 2012-07-06 12:03  Zjmainstay  阅读(748)  评论(0编辑  收藏  举报