search
HomeWeb Front-endJS TutorialWriting autoComplete plug-in in native js_javascript skills

最近有提关于下拉选项过多的时候,希望输入关键词,可以搜索内容的需求,但是之前项目太赶,所以也就没有来得及做,因为希望用原生js写一些内容,所以插件是采用了原生js写的思路如下
第一步:fnInit实现初始化一些字段
第二步:加载搜索框的div
第三步:实现search功能,删除原节点并加载新节点
第四步:点击或者回车的时候设置value
代码:
autoComplete.js

/** 
 * @summary   AutoComplete 
 * @description 输入框自动检索下拉选项 
 * @version   0.0.1 
 * @file     autoComplete.js 
 * @author   cangowu 
 * @contact   1138806090@qq.com 
 * @copyright  Copyright 2016 cangoWu. 
 * 
 * 这是一个基于原生js的自动完成搜索的下拉输入框, 
 * 可以通过移动鼠标上下键回车以及直接用鼠标点击 
 * 选中搜索的选项,在一些关键的地方都有注释 
 * 
 * 实例参见: 
 * CSDN博客:http://blog.csdn.net/wzgdjm/article/details/51122615 
 * Github:https://github.com/cangowu/autoComplete 
 * 
 */ 
(function () { 
 
  function AutoComplete() { 
    if (!(this instanceof AutoComplete)) { 
      return new AutoComplete(); 
    } 
    this.sSearchValue = ''; 
    this.index = -1; 
  } 
 
  AutoComplete.prototype = { 
    fnInit: function (option) {//初始化基本信息 
      var oDefault = { 
        id: '', //控件id 
        data: [], //数据 
        paraName: '', 
        textFiled: '', //显示的文字的属性名 
        valueFiled: '', //获取value的属性名 
        style: {}, //显示的下拉div的样式设置 
        url: '', //ajax请求的url 
        select: function () { 
        }, //选择选项时触发的事件 
      }; 
      var _option = option; 
 
      this.sId = _option.id || oDefault.id; 
      this.aData = _option.data || oDefault.data; 
      this.paraName = _option.paraName || oDefault.paraName; 
      this.sTextFiled = _option.textFiled || oDefault.textFiled; 
      this.sValueFiled = _option.valueFiled || oDefault.valueFiled; 
      this.style = _option.style || oDefault.style; 
      this.sUrl = _option.url || oDefault.url; 
      this.fnSelect = _option.select || oDefault.select; 
      this.sDivId = this.sId + new Date().getTime();//加载选项额divid 
 
      //判断如果传入了url,没有传入data数据,就ajax获取数据,否则使用data取数据 
      if (this.sUrl !== '' && this.aData.length === 0) { 
        var that = this; 
        this.util.fnGet(this.sUrl, function (data) { 
          console.log(eval(data)); 
          that.aData = eval(data); 
        }, 10); 
      } 
 
      //给aData排序 
      var sTextField = this.sTextFiled; 
      this.aData.sort(function (a, b) { 
        return a[sTextField] > b[sTextField]; 
      }); 
      //获取控件 
      this.domInput = document.getElementById(this.sId); 
      //this.domDiv = document.getElementById(this.sDivId); 
    }, 
    fnRender: function () {//渲染一些必须的节点 
      var that = this; 
      //生成一个对应的div,承载后面的一些选项的 
      if (that.sDivId) { 
        var domDiv = document.createElement('div'); 
        domDiv.id = that.sDivId; 
        domDiv.style.background = '#fff'; 
        domDiv.style.width = that.domInput.offsetWidth - 2 + 'px'; 
        domDiv.style.position = 'absolute'; 
        domDiv.style.border = '1px solid #a9a9a9'; 
        domDiv.style.display = 'none'; 
        that.util.fnInsertAfter(domDiv, that.domInput); 
 
        //加载之后才能将domDiv赋值为 
        this.domDiv = document.getElementById(this.sDivId); 
      } 
      //给input添加keyup事件 
      that.util.fnAddEvent(that.domInput, 'keyup', function (event) { 
        that.fnSearch(event); 
      }); 
    }, 
    fnSearch: function (event) { 
      //判断如果不是回车键,上键下键的时候执行搜索 
      if (event.keyCode != 13 && event.keyCode != 38 && event.keyCode != 40) { 
        this.fnLoadSearchContent(); 
        this.fnShowDiv(); 
      } else {//搜索之后监测键盘事件 
        var length = this.domDiv.children.length; 
        if (event.keyCode == 40) { 
          ++this.index; 
          if (this.index >= length) { 
            this.index = 0; 
          } else if (this.index == length) { 
            this.domInput.value = this.sSearchValue; 
          } 
          this.domInput.value = this.domDiv.childNodes[this.index].text; 
          this.fnChangeClass(); 
        } 
        else if (event.keyCode == 38) { 
          this.index--; 
          if (this.index <= -1) { 
            this.index = length - 1; 
          } else if (this.index == -1) { 
            this.obj.value = this.sSearchValue; 
          } 
          this.domInput.value = this.domDiv.childNodes[this.index].text; 
          this.fnChangeClass(); 
        } 
        else if (event.keyCode == 13) { 
          this.fnLoadSearchContent(); 
          this.fnShowDiv(); 
          //this.domDiv.style.display = this.domDiv.style.display === 'none' &#63; 'block' : 'none'; 
          this.index = -1; 
        } else { 
          this.index = -1; 
        } 
      } 
    }, 
    fnLoadSearchContent: function () { 
      //删除所有的子节点 
      while (this.domDiv.hasChildNodes()) { 
        this.domDiv.removeChild(this.domDiv.firstChild); 
      } 
      //设置search的值 
      this.sSearchValue = this.domInput.value; 
      //如果值为空的时候选择退出 
      var sTrimSearchValue = this.sSearchValue.replace(/(^\s*)|(\s*$)/g, ''); 
      if (sTrimSearchValue == "") { 
        this.domDiv.style.display = 'none'; 
        return; 
      } 
      try { 
        var reg = new RegExp("(" + sTrimSearchValue + ")", "i"); 
      } 
      catch (e) { 
        return; 
      } 
      //搜索并增加新节点 
      var nDivIndex = 0; 
      for (var i = 0; i < this.aData.length; i++) { 
        if (reg.test(this.aData[i][this.sTextFiled])) { 
          var domDiv = document.createElement("div"); 
          //div.className="auto_onmouseout"; 
          domDiv.text = this.aData[i][this.sTextFiled]; 
          domDiv.onclick = this.fnSetValue(this); 
          domDiv.onmouseover = this.fnAutoOnMouseOver(this, nDivIndex); 
          domDiv.innerHTML = this.aData[i][this.sTextFiled].replace(reg, "<strong>$1</strong>");//搜索到的字符粗体显示 
          this.domDiv.appendChild(domDiv); 
          nDivIndex++; 
        } 
      } 
    }, 
    fnSetValue: function (that) { 
      return function () { 
        that.domInput.value = this.text; 
        that.domDiv.style.display = 'none'; 
      } 
    }, 
    fnAutoOnMouseOver: function (that, idx) { 
      return function () { 
        that.index = idx; 
        that.fnChangeClass(); 
      } 
    }, 
    fnChangeClass: function () { 
      var that = this; 
      var length = that.domDiv.children.length; 
      for (var j = 0; j < length; j++) { 
        if (j != that.index) { 
          that.domDiv.childNodes[j].style.backgroundColor = ''; 
          that.domDiv.childNodes[j].style.color = '#000'; 
        } else { 
          that.domDiv.childNodes[j].style.backgroundColor = 'blue'; 
          that.domDiv.childNodes[j].style.color = '#fff'; 
        } 
      } 
    }, 
    fnShowDiv: function () { 
      if (this.domDiv.children.length !== 0) { 
        this.domDiv.style.display = this.domDiv.style.display === 'none' &#63; 'block' : 'none'; 
      } 
    }, 
    util: {//公共接口方法 
      fnInsertAfter: function (ele, targetEle) { 
        var parentnode = targetEle.parentNode || targetEle.parentElement; 
        if (parentnode.lastChild == targetEle) { 
          parentnode.appendChild(ele); 
        } else { 
          parentnode.insertBefore(ele, targetEle.nextSibling); 
        } 
      }, 
      fnAddEvent: function (ele, evt, fn) { 
        if (document.addEventListener) { 
          ele.addEventListener(evt, fn, false); 
        } else if (document.attachEvent) { 
          ele.attachEvent('on' + (evt == "input" &#63; "propertychange" : evt), fn); 
        } else { 
          ele['on' + (evt == "input" &#63; "propertychange" : evt)] = fn; 
        } 
      }, 
      fnGet: function (url, fn, timeout) { 
        var xhr = null; 
        try { 
          if (window.XMLHttpRequest) { 
            xhr = new XMLHttpRequest(); 
          } else if (Window.ActiveXObject) { 
 
            xhr = new ActiveXObject("Msxml2.Xmlhttp"); 
          } 
        } catch (e) { 
          //TODO handle the exception 
          xhr = new ActiveXObject('Microsoft.Xmlhttp'); 
        } 
        xhr.onreadystatechange = function () { 
          if (this.readyState == 4 && this.status == 200) { 
            fn.call(this, this.responseText); 
          } else { 
            setTimeout(function () { 
              xhr.abort(); 
            }, timeout); 
          } 
        }; 
        xhr.open('get', url, true); 
        xhr.send(); 
      } 
    } 
  } 
 
  window.AutoComplete = function (option) { 
    var aOption = Array.prototype.slice.call(arguments); 
    for(var i=0;i<aOption.length;i++){ 
      var autoComplete = new AutoComplete(); 
      autoComplete.fnInit(aOption[i]); 
      autoComplete.fnRender(); 
    } 
  } 
 
})(window); 

index.html

<!DOCTYPE html> 
<html lang="en"> 
<head> 
  <meta charset="UTF-8"> 
  <title>Title</title> 
</head> 
<body> 
 
<div> 
<input type="text" id="txtTest"> 
</div> 
<br> 
<div> 
<input type="text" id="txtTest1"> 
</div> 
 
<script src="autoComplete.js"></script> 
<script> 
 
  window.onload = function () { 
    var option = { 
      id: 'txtTest', //控件id 
      data: [{ 
        "id": "1", 
        "name": "aaaaa" 
      }, { 
        "id": "2", 
        "name": "bbbbb" 
      }, { 
        "id": "2", 
        "name": "bbb吴bb" 
      }, { 
        "id": "2", 
        "name": "bbbzbb" 
      }], 
      paraName: 'name', 
      textFiled: 'name', //显示的文字的属性名 
      valueFiled: 'id', //获取value的属性名 
      select: function (val, text) { 
        alert(val + '' + text); 
      } //选择选项时触发的事件 
    }; 
    var option1 = { 
      id: 'txtTest1', //控件id 
      url: 'data.json', //数据 
      paraName: 'name', 
      textFiled: 'name', //显示的文字的属性名 
      valueFiled: 'id', //获取value的属性名 
      select: function (val, text) { 
        alert(val + '' + text); 
      } //选择选项时触发的事件 
    }; 
    AutoComplete(option,option1); 
 
  } 
 
</script> 
</body> 
</html> 

data.json

[ 
 { 
  "id": "1", 
  "name": "aaaaa" 
 }, 
 { 
  "id": "2", 
  "name": "bbbbb" 
 }, 
 { 
  "id": "3", 
  "name": "ccccc" 
 } 
] 

以上就是本文的全部内容,希望对大家学习javascript程序设计有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version