---
title: "对js中startWith和endWith的扩展"
author: "Lives"
date: "2012-12-26T13:03:26Z"
tags: []
categories: ["Web前端"]
slug: "js-startwith-endwith"
draft: false
lastmod: "2012-12-26T13:03:26Z"
id: 1754374
url: "https://lives.iteye.com/blog/1754374"
---

对js中startWith和endWith的扩展

 

&lt;script language="JavaScript"&gt;  
 function closeUl(code){  
  alert(code);  
  var list = document.getElementsByTagName("li");  
  for(var i=0;i&lt;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&gt;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&gt;this.length)  
   return false;  
  if(this.substr(0,str.length)==str)  
     return true;  
  else  
     return false;  
  return true;  
 }  
&lt;/script&gt;