现在的位置: 首页 > 综合 > 正文

js中startWith和endWith的扩展

2018年05月21日 ⁄ 综合 ⁄ 共 958字 ⁄ 字号 评论关闭
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>

 <body>
    <input name="searchContent" title="改变值后,单击这里" id="searchContent" type="text" value="abcdef" onclick="test(this.value);">
<SCRIPT type="text/javascript" language="JavaScript">
function test(value){
 var a  = value.startWith("abc");
 alert("a : "+a);
 var b  = value.endWith("def");
 alert("b : "+b);
}
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>

 </body>
</html>

抱歉!评论已关闭.