对js中startWith和endWith的扩展
<script language="JavaScript">
function closeUl(code){
alert(code);
var list = document.getElementsByTagName("li");
for(var i=0;i<list.length;i++){
var curLi = list[i].id;
if(curLi.indexOf(code) != -1 && curLi.length != code.length && curLi.startWith
(code)){
alert(curLi);
if(document.getElementById(curLi).style.display == "none"){
if(curLi.length == code.length+5){
document.getElementById(curLi).style.display = "block";
}
}else{
document.getElementById(curLi).style.display = "none";
}
}
}
}
String.prototype.endWith=function(str){
if(str==null||str==""||this.length==0||str.length>this.length)
return false;
if(this.substring(this.length-str.length)==str)
return true;
else
return false;
return true;
}
String.prototype.startWith
=function(str){
if(str==null||str==""||this.length==0||str.length>this.length)
return false;
if(this.substr(0,str.length)==str)
return true;
else
return false;
return true;
}
</script>
本文介绍了一种JavaScript中对字符串方法startWith和endWith的实现方式,并展示了如何使用这些方法来操作DOM元素。通过扩展String原型,使得字符串能够方便地判断是否以指定子串开头或结尾。
1267

被折叠的 条评论
为什么被折叠?



