Jquery实现仿京东商城省市联动菜单

本文实例讲述了Jquery实现仿京东商城省市联动菜单的简单实例代码。分享给大家供大家参考。具体如下:
运行效果截图如下:

Jquery实现仿京东商城省市联动菜单

具体代码如下:

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <link href="https://www.jb51.net/assets/css/bootstrap.min.css"> <link href="https://www.jb51.net/css/custom-theme/jquery-ui-1.10.0.custom.css"> <style> .region li{ float: left; width: 15%; list-style: none; line-height: 30px; padding: 2px 15px; white-space: nowrap; } .region li a{ text-decoration:none; font-size:14px; } .region li a:hover{ background-color: #5bb75b; } </style> </head> <body> <input type="text"> <div> <div> <ul> <li><a href="#tabs-1">省份</a></li> </ul> <div> <ul> </ul> </div> </div> </div> <script src="https://www.jb51.net/assets/js/jquery-1.9.0.min.js"></script> <script src="https://www.jb51.net/assets/js/jquery-ui-1.10.0.custom.min.js"></script> <script> $(function(){ var provinces = ['北京', '天津', '山东', '河南', '河北', '山西', '湖北', '湖南', '江西', '浙江', '上海', '安徽', '广东', '广西', '福建', '宁夏', '重庆', '四川', '西藏', '海南', '香港', '澳门', '内蒙古', '陕西', '甘肃', '黑龙江', '辽宁', '吉林']; var city = ['广州', '深圳', '东莞']; var county = ['宝安', '南山', '福田', '罗湖']; $('.address').bind('focus', function(){ var $this = $(this); $('#addressInfo').css({ top: $this.offset().top + $this.outerHeight(), left : $this.offset().left }).show(); }); var $provinces_li = $('.region>ul'); $.each(provinces, function(e){ $provinces_li.append('<li><a href="javascript:void(0);">'+this+'</a></li>'); }); var i = 0; $('.area') .tabs() .on('click', '.provinces', function(){ //获取当前对象 var $this = $(this), $tabs = $this.parents('.selectAddress'), //追加DIV $div = $('<div></div>'), $ul = $('<ul></ul>'); //追加ul $tabs.children(':eq(0)').children(':gt(0)').remove(); $tabs.children('div:gt(0)').remove(); //each遍历,赋值 //最好是这样,code、name $ul.append('<li><a href="javascript:void(0);">'+this.name+'</a><input type="hidden" value="'+this.code+'"/></li>'); $.each(city, function(){ $ul.append('<li><a href="javascript:void(0);">'+this+'</a></li>'); }); $tabs.children('ul').append('<li><a href="#tabs-2">市区</a></li>'); $tabs.append($div.addClass('region').append($ul)); $tabs .tabs( "refresh" ) .tabs('option', 'active', 1) .data('address', $this.text()); }) .on('click', '.city', function(){ var $this = $(this), $tabs = $this.parents('.selectAddress'), $div = $('<div></div>'), $ul = $('<ul></ul>'); $tabs.children('ul').children(':eq(2)').remove(); $tabs.children('div:eq(2)').remove(); i++; if(i == 1){ //判断是否有下级 $.each(county, function(){ $ul.append('<li><a href="javascript:void(0);">'+this+'</a></li>'); }); $tabs.children('ul').append('<li><a href="#tabs-3">县区</a></li>'); $tabs.append($div.addClass('region').append($ul)); $tabs .tabs( "refresh" ) .tabs('option', 'active', 2) .data('address', $tabs.data('address')+'https://www.jb51.net/'+$this.text()); }else{ //获取值并赋值至文本框中 $('.address').val($tabs.data('address')+'https://www.jb51.net/'+$this.text()); $tabs.parent().hide(); } }) .on('click', '.county', function(){ var $this = $(this), $tabs = $this.parents('.selectAddress'); $('.address').val($tabs.data('address')+'https://www.jb51.net/'+$this.text()); $tabs.parent().hide(); }) ; $(document).bind('click', function(e){ var $target = $(e.target), addressInfo = $('#addressInfo'); if(!$target.hasClass('selectAddress') && $target.parents('.selectAddress').size() == 0 && !$target.is($('.address')) && addressInfo.is(':visible')){ $('#addressInfo').hide(); } }); }) </script> </body> </html>

源码下载: 《Jquery实现仿京东商城省市联动菜单》

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgzjjp.html