JavaScript+node实现三级联动菜单

 更新时间:2022年07月28日 09:42:36   作者:LoveyL0201  
这篇文章主要为大家详细介绍了JavaScript+node实现三级联动菜单,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了JavaScript+node实现三级联动菜单的具体代码,供大家参考,具体内容如下

项目分析

1、效果

2、前端

=>面向对象

=>首先分析下拉结构,构造相应的结构和样式

=>绑定事件,点击,鼠标滑过等

=>导入写好的 js 文件 构造菜单项、

=>使用ajax请求服务端 获取数据

=>用每次获取的数据动态生成页面结构

3、服务端

=>接口文档(定义接口,服务地址,端口}

=>存储数据

=>创建服务器

=>接收前端的ajax请求,响应数据

代码

1、面向对象代码

export default class Select{
    //添加css样式
    static  addCSS(selector,style,title){
        if(title===undefined) title="xietian";
        var arr=Array.from(document.styleSheets);
        var  styleSheet=arr.reduce(function(value,item){
            if(item.title===title)return item;
            return value;
        },null);
        if(!styleSheet){
            var s=document.createElement("style");
            s.title=title;
            document.head.appendChild(s);
            styleSheet=document.styleSheets[document.styleSheets.length-1];
        }
        var str="";
        for(var prop in style){
            str+=prop.replace(/[A-Z]/g,function(value){
                return "-"+value.toLowerCase();
            })+":"+(typeof style[prop]==="number" ? style[prop]+"px" : style[prop])+";";
        }
        if(styleSheet.addRule){
            styleSheet.addRule(selector,str,styleSheet.cssRules.length);
        }else{
            styleSheet.insertRule(selector+"{ "+str+" }",styleSheet.cssRules.length);
        }
    }

//定义全局变量
    button;
    ul;
    class;
    constructor(parent,_class) {
        // this.id = _id;
        this.class = _class;
        this.elem = this.createEle();
        this.appendTo(parent);
    }
    //创建元素
    createEle() {
        const div = document.createElement('div');
        div.className = "dropdown " + this.class;
        this.button = document.createElement('button');
        this.button.className = "btn btn-default dropdown-toggle";
        this.button.addEventListener('click',e=>this.clickHander(e))
        this.content = document.createElement('span');
        this.content.innerText = "请选择";
        const carte = document.createElement('span');
        this.content.className = 'content';
        carte.className = 'carte';
        this.button.appendChild(this.content);
        this.button.appendChild(carte);
        this.ul = document.createElement('ul');
        this.ul.className = "dropdown-menu";
        this.button.appendChild(this.ul);
        div.appendChild(this.button)
        return div;
    }
    //添加点击事件
    clickHander(e) {
        this.ul.classList.toggle('on');
    }
   //增加li
    addli(list) {
        if(!list) return;
        const frg = document.createDocumentFragment();
        list.forEach(item=>{
            const li = document.createElement('li');
            li.innerHTML = item;
            li.addEventListener('click',e=>this.liHandler(e));

            frg.appendChild(li);
        })
        this.ul.appendChild(frg);
    }
    //给li添加的点击事件
    liHandler(e) {
        this.content.innerHTML = e.target.innerText;

    }
    //添加css样式
    addCSS() {
        Select.addCSS(".dropdown",{
            position: "relative",
            boxSizing: "border-box",
            width: "8.33333333%",
            display:"inline-block",
            minHeight: "1px",
            paddingRight: "15px",
            paddingLeft: "15px",
            fontSize: "14px",
            lineHeight: "1.42857143",
            color: "#333333",
        })
        Select.addCSS(".btn",{
            display:'inlineBlock',
            marginBottom:'0',
            outline:"none",
            fontWeight:'normal',
            textAlign:'center',
            whiteSpace:'nowrap',
            verticalAlign:'middle',
            touchAction:'manipulation',
            cursor:'pointer',
            backgroundImage:'none',
            border:'1px solid transparent',
            padding:'6px 12px',
            fontSize:'14px',
            lineHeight:'1.42857143',
            borderRadius:'4px',
            userSelect:'none',
        })
        Select.addCSS(".btn-default",{
           color:'#333',
            backgroundColor:'#fff',
            borderColor:'#ccc',
        })
        Select.addCSS(".btn-default:hover",{
            color:'#333',
            backgroundColor:'#e6e6e6',
            borderColor:'#adadad',
        })
        Select.addCSS(".carte",{
            display:'inline-block',
            width:'0',
            marginLeft: "0",
            height:'0',
            verticalAlign:'middle',
            borderTop:'4px dashed',
            borderRight:'4px solid transparent',
            borderLeft:'4px solid transparent',
        })
        Select.addCSS(".dropdown-menu",{
            position:'absolute',
            top:'100%',
            left:'15px',
            height:'200px',
            overflowY:'scroll',
            zIndex:'1000',
            display:'none',
            float:'left',
            minWidth:'83px',
            padding:'5px 0',
            margin:'2px 0 0',
            fontSize:'14px',
            textAlign:'left',
            listStyle:'none',
            backgroundColor:'#fff',
            backgroundClip:'paddingBox',
            border:'1px solid #ccc',
            border:'1px solid rgba(0, 0, 0, 0.15)',
            borderRadius:'4px',
            boxShadow:'0 6px 12px rgba(0, 0, 0, 0.175)',
        
        })
        Select.addCSS(".on", {
            display:"block!important",
        })
        Select.addCSS('li',{
            textAlign:"center",
        })
        Select.addCSS('li:hover',{
            backgroundColor:'#e6e6e6',
        })
    }
    //添加到html结构中的位置
    appendTo(parent) {
        if(typeof parent == 'string') parent=document.querySelector(parent);
        parent.appendChild(this.elem);
        this.addCSS();
    }

}

2、向服务器请求ajax

<script type="module">
        import Select from "./select.js";
        // var list = ["北京","上海","广州","深圳","武汉"];

        let citylist,provincelist,countylist,province_content,content_county;

        var sel_p = new Select('.box','province');

        ajax("city.json",(res)=>{
             provincelist = Object.keys(res);
            sel_p.addli(provincelist);
            sel_p.elem.addEventListener('click',clickHandler)
         province_content = document.querySelector('.province .content');
            county() 
        })

        var sel_c = new Select('.box','city');

        function clickHandler(e) {
            if(e.target.constructor!== HTMLLIElement) return;
            ajax(`http://10.9.72.252:4001/province/?province=${e.target.innerText.trim()}`,function(res){
                 citylist = Object.keys(res.city)
                sel_c.addli(citylist);
                const province = document.querySelector('.province ul');
                province.addEventListener('click',cityHandler);
            })
        }
        

        function cityHandler(e) {
            const content_city = document.querySelector('.city .content');
            const li = document.querySelectorAll('.city li');
            const content_county  = document.querySelector('.county .content');
            content_county.innerText=content_city.innerText = "请选择";
            li.forEach(item=>{
                item.remove();
            })
        }
            //获取区县数据
       function county() {
           var sel_conuty = new Select('.box','county');
           sel_c.elem.addEventListener('click',e=>{
            if(e.target.constructor!== HTMLLIElement) return;
            ajax(`http://10.9.72.252:4001/city/?province=${province_content.innerText.trim()}&city=${e.target.innerText.trim()}`,function(res){
                // console.log(res); 
                countylist =res.county
                sel_conuty.addli(countylist);
                const city = document.querySelector('.city ul');
                city.addEventListener('click',countyHandler);
            })
           })
       }    
        
       function countyHandler(e) {
        const lis = document.querySelectorAll('.county li');
        const content_coun = document.querySelector('.county .content');
        content_coun.innerText = "请选择";
        lis.forEach(item=>{
                item.remove();
            })

       }
       //封装一个简单的
        function ajax(url, fn) {
                // XMLHttpRequest对象用于在后台与服务器交换数据
                var xhr = new XMLHttpRequest();
                xhr.open('GET', url, true)
                xhr.onreadystatechange = function () {
                    if(xhr.readyState == 4 && xhr.status ==200) {
                        //从服务器获得数据
                        fn.call(this, JSON.parse(xhr.responseText));
                    }
                };
                //发送数据
                xhr.send();
            }
</script>

3、服务端

const http = require('http');
const querystring= require('querystring');
const city = require("../city.json");
const server = http.createServer(function(req, res) {
    res.writeHead(200,{
        "content-type":"text/html;charset=utf-8",
        "Access-Control-Allow-Origin":"*",
        "Access-Control-Allow-Headers":"*",
        //请求头跨域 如果请求头发生修改并且非同源,就需要申请请求头跨域
    });
    req.on('data',function(_data) {
        data=_data
    })
    req.on('end',function () {
        // type是接口名称
        console.log(req.url);
        var type=req.url.trim().split("?")[0].replace(/\//g,"");
        console.log(type);
        if(req.method.toLowerCase()==="get"){
            if(req.url.includes("favicon.ico")) return res.end();//如果get请求的是图标直接返回空跳出
            // 如果是get请求,就从url中重新获取数据存入data变量
            data=req.url.includes("?") ? req.url.split("?")[1] : "";
        }
        // 首先判断是否可以通过JSON解析,如果通过JSON直接转换,如果不能就是querystring解析
        try{
            data=JSON.parse(data);
        }catch(e){
            data=data ? querystring.parse(data) : {};
            console.log(data);

        }
        let o = {}
        switch(type){
            case 'province':
                o = {};
                o.city = obj[data.province];
                break;
            case 'city':
                o.city = obj[data.province];
                o.county = o.city[data.city];
                break;
        }
        res.write(JSON.stringify(o));
        res.end();
    })
})
//监听4001端口
server.listen(4001,"你的地址",function() {
    console.log("服务器开启成功^_^");
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • js关闭浏览器窗口及检查浏览器关闭事件

    js关闭浏览器窗口及检查浏览器关闭事件

    js关闭浏览器窗口,不弹出提示框。支持ie6+,火狐,谷歌等浏览器,下面以一个示例为大家详细介绍下具体的实现方法,感兴趣的朋友可以参考下
    2013-09-09
  • JS实现的N多简单无缝滚动代码(包含图文效果)

    JS实现的N多简单无缝滚动代码(包含图文效果)

    这篇文章主要介绍了JS实现的N多简单无缝滚动代码,包含了文字及图文等多种滚动效果,涉及JavaScript递归调用及定时函数触发实现页面元素动态变换的相关技巧,需要的朋友可以参考下
    2015-11-11
  • 用javascript实现模拟火焰

    用javascript实现模拟火焰

    用javascript实现模拟火焰...
    2007-10-10
  • JS组件库AlloyTouch实现图片轮播过程解析

    JS组件库AlloyTouch实现图片轮播过程解析

    这篇文章主要介绍了JS组件库AlloyTouch实现图片轮播组件过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • TypeScript对于Duck类型和模块命名空间应用

    TypeScript对于Duck类型和模块命名空间应用

    这篇文章主要介绍了TypeScript对于Duck类型和模块命名空间应用,Duck类型是一种动态类型和多态形式,在duck类型中,重点是对象的行为可以做什么,而不是对象所属的类型
    2022-08-08
  • JS 实现百度搜索功能

    JS 实现百度搜索功能

    这篇文章给大家介绍了js实现百度搜索功能,代码分为html部分和css折叠样式部分,具体实现代码大家参考下本文
    2018-02-02
  • layer.prompt输入层的例子

    layer.prompt输入层的例子

    今天小编就为大家分享一篇layer.prompt输入层的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-09-09
  • bootstrap table实现双击可编辑、添加、删除行功能

    bootstrap table实现双击可编辑、添加、删除行功能

    这篇文章主要为大家详细介绍了bootstrap table实现双击可编辑、添加、删除行功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • 原生JavaScript实现留言板

    原生JavaScript实现留言板

    这篇文章主要为大家详细介绍了原生JavaScript实现留言板,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-01-01
  • JavaScript defineProperty如何实现属性劫持

    JavaScript defineProperty如何实现属性劫持

    双向数据绑定的核心方法,主要是做数据劫持操作(监控数据变化),下面这篇文章主要给大家介绍了关于JavaScript defineProperty如何实现属性劫持的相关资料,需要的朋友可以参考下
    2021-07-07

最新评论