<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
字符串:<input type="text" id="txt"> 索引字符串:<input type="text" id="index">
<button id="btn">从前面开始索引</button>
<button id="btn1">从后面开始索引</button>
</body>
</html>
<script>
function $(id) {return document.getElementById(id);}
$("btn").onclick = function() {
alert($("txt").value.indexOf($("index").value) );
}
$("btn1").onclick = function() {
alert($("txt").value.lastIndexOf($("index").value) );
}
</script>
运行如下