关键词搜索

源码搜索 ×
×

JavaScript笔记-使用Jsonp获取百度搜索提示

发布2022-05-25浏览443次

详情内容

要实现的效果就是这样的:

原理说白了就是发送一个get请求:

然后把数据放到list里面就可以了。

这里使用jsonp,因为这个要在客户端发起的请求。

关键代码如下:

  1. function getSuggestion(url, word){
  2. let wordObject = {'wd' : word, 'cb' : 'suggestionCallBack'};
  3. $.ajax({
  4. async: false,
  5. url: url,
  6. type: 'GET',
  7. dataType: 'jsonp',
  8. jsonp: 'cb',
  9. // jsonpCallback: 'suggestionCallBack',
  10. data: wordObject,
  11. timeout: 5000,
  12. success: function (data) {
  13. },
  14. error: function (data) {
  15. // alert(data);
  16. }
  17. })
  18. }
  19. //获取控件ID
  20. function getId(id) {
  21. return (typeof id == 'string') ? document.getElementById(id) : id
  22. };
  23. function suggestionCallBack(data){
  24. let urls = data["s"];
  25. let html = "<ul id=\"allSitesBoxContent\" class=\"list-group text-start\" style=\"position: absolute\">";
  26. if (urls) {
  27. let urlList = urls;
  28. for (let i = urlList.length - 1; i >= 0; i--) {
  29. let textVal = urlList[i];
  30. if ($.trim(textVal) != "" && $.trim(textVal) != "undefined") {
  31. html += "<li class='list-group-item bg-transparent text-white border-1 border-white' >" + textVal + "</li>";
  32. }
  33. }
  34. }
  35. html += "</ul>";
  36. getId("ulFather").innerHTML = html;
  37. controllerSelect();
  38. }
  39. $('#searchInput').bind('input propertychange', function () {
  40. let word = $.trim($("#searchInput").val());
  41. if(word == ""){
  42. getId("ulFather").innerHTML = "";
  43. return;
  44. }
  45. getSuggestion('http://suggestion.baidu.com/su', word);
  46. })

这样就设置好了list,下面是按下键盘,选中,然后回车跳转:

  1. function jumpSearch(){
  2. let searchInput = document.getElementById('searchInput');
  3. window.location.href = searchArr[picIndex] + searchInput.value;
  4. }

以及:

  1. ;
  2. function controllerSelect() {
  3. let list = document.getElementById('allSitesBoxContent').getElementsByTagName('li');
  4. let index = -1;
  5. let liLength = list.length - 1;
  6. function addIndex() {
  7. index = index >= liLength ? 0 : ++index;
  8. }
  9. function reduceIndex() {
  10. index = index <= 0 ?liLength : --index;
  11. return index;
  12. }
  13. function setLiColorByClass(){
  14. for(let i =0; i < list.length; i++){
  15. if(i == index){
  16. list[i].className = 'list-group-item border-1 list-group-item-info';
  17. }
  18. else{
  19. list[i].className = 'list-group-item bg-transparent text-white border-1 border-white';
  20. }
  21. }
  22. }
  23. let searchInput = document.getElementById('searchInput');
  24. searchInput.onkeydown = function(e){
  25. if(e.key == "Enter"){
  26. return false;
  27. }
  28. }
  29. document.onkeydown = function (e) {
  30. if(e.key == 'ArrowUp'){
  31. reduceIndex();
  32. setLiColorByClass();
  33. }
  34. else if(e.key == 'ArrowDown'){
  35. addIndex();
  36. setLiColorByClass();
  37. }
  38. else if(e.key == "Enter"){
  39. console.log(index);
  40. if(index > 0){
  41. searchInput.value = list[index].innerHTML;
  42. let father = document.getElementById('allSitesBoxContent');
  43. if(father != null){
  44. father.remove();
  45. }
  46. }
  47. else {
  48. if(searchInput.value != ""){
  49. console.log(searchArr[picIndex] + searchInput.value);
  50. window.location.href = searchArr[picIndex] + searchInput.value;
  51. return false;
  52. }
  53. }
  54. searchInput.onkeydown = function(e){
  55. if(e.key == "Enter"){
  56. console.log(searchArr[picIndex]);
  57. if(searchInput.value != ""){
  58. console.log(searchArr[picIndex] + searchInput.value);
  59. window.location.href = searchArr[picIndex] + searchInput.value;
  60. return false;
  61. }
  62. return true;
  63. }
  64. }
  65. }
  66. }
  67. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载