<script>
//字符截取(需要的字符长度)
function cut_str(need_str_length){
var bag_set = document.getElementsByTagName('p'); //alert(bag_set); //获取标签集合
for(var i = 0; i < bag_set.length; i++)
{
var bag = bag_set[i]; //alert(bag); //获取标签
var bag_text = bag_set[i].innerHTML; //alert(bag_text); //获取标签文本
var txt = bag_text.substring(0,need_str_length); //alert(txt); //截取 只要10个字符
bag.innerText = txt; //标签 文本赋值
}
}
</script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="resources/js/tools.js"></script>
</head>
<body onload="cut_str(5);">
<p>数字1234567890</p>
<p>字母ABCDEFG</p>
<p>汉字我了个去</p>
</body>
</html>
效果图: