当前位置: 代码迷 >> JavaScript >> 对js中startWith跟endWith的扩展
  详细解决方案

对js中startWith跟endWith的扩展

热度:170   发布时间:2012-12-29 10:28:09.0
对js中startWith和endWith的扩展

对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>