使用JS写简单查询页面?

JavaScript011

使用JS写简单查询页面?,第1张

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Document</title>

<script>

function select(){

var data= document.getElementById("str").value

if (data=="" || data==undefined) {

alert('请输入查询的数据')

}

switch (data) {

case 'a':

window.location.href="\a.html"

break

case 'b':

window.location.href="\b.html"

break

default:

window.location.href="\c.html"

break

}

}

</script>

</head>

<body>

<div>

<input type="text" id="str">

<button onclick="select()">搜索</button>

</div>

</body>

</html>

在 js里面查询某个字符串,方法如下:

定义一个方法

function findStr(srcStr,findStr){

    var index=-1

    if((index=srcStr.indexOf(findStr))!=-1){

        alert(srcStr+"查找到"+findStr+"在索引位置"+index)

    }

}

调用该方法,传入源字符串,和要查找的字符串:

findStr("hello23","23")

结果:

浏览器查看。

用Chrome、火狐等(其它浏览器操作类同),打开浏览器后,按F12。或者打开开发者工具,可以查看相应的html、css、js等内容。

效果图如下(查看源码)