JavaScript自定义函数实现字符串StartWith和EndWith
2017-01-29 21:17:22 访问(1849) 赞(0) 踩(0)
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
.btn
{
cursor: pointer;
height: 25px;
width: 80px;
line-height: 23px;
margin: 0px 4px;
padding: 0 8px; *padding:02px;text-align:center;display:inline;border-radius:2px;font-size:12px;}</style>
</head>
<body>
<h1>
JavaScript自定义函数实现字符串StartWith和EndWith</h1>
输入:<br />
<input name="txt_Input" type="text" value="www.slowx.net" id="txt_Input" style="width:99%;" />
<br />
字符串:<br />
<input name="txt_SubInput" type="text" value="www." id="txt_SubInput" style="width:99%;" />
<br />
<br />
<input type="button" id="btn_StartWith" class="btn" value="StartWith" onclick="OkClick(true)" />
<input type="button" id="btn_EndsWith" class="btn" value="EndsWith" onclick="OkClick(false)" />
<br />
<br />
输出:<br />
<input name="txt_Output" type="text" id="txt_Output" style="width:99%;" /> </body>
</html>
<script language="javascript" type="text/javascript">
function StrStartsWith(str, theChar) {
// StartWith判断 //
if (str == null || str == "" || theChar == null || theChar == "")
return false;
var idx = str.indexOf(theChar);
if (idx == 0)
return true;
else
return false;
}
function StrEndsWith(str, theChar) {
// EndsWith判断 //
if (str == null || str == "" || theChar == null || theChar == "")
return false;
var idx = str.lastIndexOf(theChar);
var strLen = str.length;
var theCharLen = theChar.length;
if (idx == strLen - theCharLen)
return true;
else
return false;
}
function OkClick(isStart) {
var inputCtrl = document.getElementById("txt_Input");
var subInputCtrl = document.getElementById("txt_SubInput");
var outputCtrl = document.getElementById("txt_Output");
var theValue = inputCtrl.value;
var theChar = subInputCtrl.value;
var theResult = false;
if (isStart)
theResult = StrStartsWith(theValue, theChar);
else
theResult = StrEndsWith(theValue, theChar);
outputCtrl.value = theResult;
}
</script>
标签:
JavaScript自定义函数实现字符串StartWith和EndWith 


上一条:
下一条:
相关评论
发表评论