直白一点,就是这样的效果:
这里需要实现的是局部数据传输,而不是整个页面的加载,就用到了ajax
这里简单的实现了这个功能
代码如下:
HTML部分
<!DOCTYPE html> <html lang="en"> <head> <Meta charset="UTF-8"> <Meta name="viewport" content="width=device-width,initial-scale=1.0"> <Meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script> function showres(res){ //不输入或者删除 关联词位置不显示 退出showres() if(res.length==0){ document.getElementById(‘textres‘).innerHtml=""; return; } //浏览器兼容性 // var xmlhttp; if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); }else{ xmlhttp = new ActiveXObject(‘Microsoft.XMLHTTP‘); } xmlhttp.onreadystatechange = function (){ if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ document.getElementById(‘textres‘).innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","guanlian.PHP?q="+res,true); xmlhttp.send(); } </script> </head> <body> <h3>请输入:</h3> <form> <input type="text" onkeyup="showres(this.value)"> <p><span id=‘textres‘></span></p> </form> </body> </html>
PHP部分:
<?PHP $a[] = ‘apple‘; $a[] = ‘auto‘; $a[] = ‘apache‘; $a[] = ‘apart‘; $a[] = ‘black‘; $a[] = ‘blue‘; $a[] = ‘back‘; $a[] = ‘bus‘; $a[] = ‘cup‘; $a[] = ‘case‘; $a[] = ‘can‘; $a[] = ‘来了‘; $a[] = ‘来了老弟‘; $a[] = ‘有来有去‘; $a[] = ‘不会来‘; //获取参数q $q = $_GET[‘q‘]; //是否有值 if(strlen($q)>0){ $relation=""; for($i=0;$i<count($a);$i++){ if(strtolower($q)== strtolower(substr($a[$i],strlen($q)))){ if($relation==""){ $relation=$a[$i]; }else{ $relation=$relation.‘<br>‘.$a[$i]; } } } } if($relation==""){ $response=‘没有关联词‘; }else{ $response=$relation; } echo $response;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。