canRun
<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"> functioncustomSubstr(str,br){ if(br<str.length&&str.charAt(br) != ' ') br=str.indexOf(' ',br); //判断当前字符非空格,则查找向后第一个空格 returnstr.substr(0,br); } varstr = "I want this effect, not that one."; document.getElementById('substring').innerHTML = '截取22位:'+customSubstr(str,22); </script> </html>