题目:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>交错01串</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
<style>
input{
width: 300px;
height: 30px;
border: 1px cadetblue solid;
border-radius: 10px;
}
button{
width: 100px;
height: 30px;
color: aliceblue;
border: 1px cadetblue solid;
border-radius: 5px;
background: cornflowerblue;
}
h2{
color: darkslategray;
font-family: "楷体";
}
</style>
</head>
<body>
<h2>交错01串</h2>
<input type="text" onkeyup="value=value.replace(/[^\[01]$]/g,'')"/>
<button>判断</button>
<script>
var re = /[^01]/;
var a = [],b = [];
var j = 1;
$('button').on('click',function () {
if ($('input').val().length < 1 || $('input').val().length >= 50) {
alert("长度必须在1-50之间")
} else{
if (re.test($('input').val())) {
alert("只能输入0,1")
} else{
a = $('input').val().split('');
for (var i=0 ;i<a.length-1;i++) {
if (a[i] == a[i+1]) {
b.push(j);
j=1;
} else{
j++;
}
}
b.push(j);
alert(Math.max.apply(null, b));
j=1;
b=[];
}
}
})
</script>
</body>
</html>
网友评论