请求不了 JSON 是怎么回事
<!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"> <link rel="stylesheet" href="./css/base.css"> <link rel="stylesheet" href="./font/iconfont.css"> <title>dropDown</title> <style> .dropdown { position: relative; /* margin: 150px; */ } .dropdown-toggle { position: relative; z-index: 2; } /* 下拉箭头 */ .dropdown-arrow { display: inline-block; /* text-align: center; */ vertical-align: text-top; } .dropdown-layer { display: none; overflow: hidden; position: absolute; z-index: 1; } .dropdown-left { left: 0; right: auto; } .dropdown-right { right: 0; left: auto; } /* menu dropdown */ .menu .dropdown-toggle { display: block; height: 100%; padding: 0 16px 0 12px; border-left: 1px solid #f3f5f7; border-right: 1px solid #f3f5f7; } .menu .dropdown-arrow { margin-left: 5px; } .menu .dropdown-layer { /* top: 44px; */ top: 100%; border: 1px solid #cdd0d4; /* border-top: none; */ background-color: #fff; } .menu-item { display: block; height: 30px; padding: 0 12px; line-height: 30px; white-space: nowrap; color: #4d555d; } .menu-item:hover { background-color: #f3f5f7; } /* css hover 实现显示隐藏 */ .menu-active .dropdown-toggle { background-color: #fff; border-color: #cdd0d4; } /* 下拉箭头效果 */ [class *="-active"] .dropdown-arrow { -o-transform: rotate(180deg); -ms-transform: rotate(180deg); -moz-transform: rotate(180deg); -webkit-transform: rotate(180deg); transform: rotate(180deg); } .transition { -o-transition: all 0.5s; -ms-transition: all 0.5s; -moz-transition: all 0.5s; -webkit-transition: all 0.5s; transition: all 0.3s; } /* .menu-active .dropdown-layer { display: block; } */ /* 显示隐藏效果 */ .fadeOut { visibility: hidden !important; opacity: 0 !important; } .slideUpDownCollapse { height: 0 !important; padding-top: 0 !important; padding-bottom: 0 !important; } .slideLeftRightCollapse { width: 0 !important; padding-left: 0 !important; padding-right: 0 !important; } </style> </head> <body> <div class="menu dropdown fl" data-active="menu"> <a href="javascript:;" class="dropdown-toggle">我的慕淘<i class="iconfont dropdown-arrow transition"></i></a> <ul class="dropdown-layer dropdown-left"> <li><a href="###" class="menu-item">已买到宝贝</a></li> <li><a href="###" class="menu-item">我的足迹</a></li> </ul> </div> <button id="btn-show">点我显示</button id="btn-show"> <button id="btn-hide">点我隐藏</button> <script src="./js/jquery.js"></script> <script src="./js/transition.js"></script> <script src="./js/showHide.js"></script> <script src="./js/dropDown.js"></script> <script> $('.dropdown').on('dropdown-show', function (e) { $.getJSON('./js/dropdown.json', function (data) { console.log(data) }) }); $('.dropdown').dropdown({ event: 'hover', css3: true, js: true, animation: 'fade', delay: 0, active: 'menu' }); $('#btn-show').on('click', function () { $('.dropdown').dropdown('show'); }); $('#btn-hide').on('click', function () { $('.dropdown').dropdown('hide'); }); </script> </body> </html>
(function ($) { 'use strict'; // Constructor function Dropdown($elem, options) { this.$elem = $elem; this.options = options; this.$layer = this.$elem.find('.dropdown-layer'); this.activeClass = options.active + '-active'; this._init(); } Dropdown.DEFAULTS = { event: 'hover', css3: true, js: true, animation: 'fade', delay: 0, active: 'dropdown' }; Dropdown.prototype._init = function () { var self = this; this.$layer.showHide(this.options); this.$layer.on('show shown hide hidden', function (e) { self.$elem.trigger('dropdown-' + e.type) }); if (this.options.event === 'click') { this.$elem.on('click', function (e) { self.show(); e.stopPropagation(); }); $(document).on('click', $.proxy(this.hide, this)); } else { this.$elem.hover($.proxy(this.show, this), $.proxy(this.hide, this)); }; }; Dropdown.prototype.show = function () { var self = this; if (this.options.delay) { this.timer = setTimeout(function () { _show(); }, this.options.delay); } else { _show(); } function _show() { self.$elem.addClass(self.activeClass); self.$layer.showHide('show'); } }; Dropdown.prototype.hide = function () { if (this.options.delay) { clearTimeout(this.timer); }; this.$elem.removeClass(this.activeClass); this.$layer.showHide('hide'); }; $.fn.extend({ dropdown: function (option) { return this.each(function () { var $this = $(this), dropdown = $this.data('dropdown'), options = $.extend({}, Dropdown.DEFAULTS, $this.data(), typeof option === 'object' && Option); if (!dropdown) { $this.data('dropdown', dropdown = new Dropdown($this, options)); } if (typeof dropdown[option] === 'function') { dropdown[option](); } }); } }); })(jQuery);
[ { "url": "###", "name": "已买到宝贝" }, { "url": "###", "name": "我的足迹" }, { "url": "###", "name": "我的优惠券" } ]
报错
34
收起
正在回答
4回答
同学你好,报错信息跨域问题,原因是json文件不允许本地访问,需要放在服务器下。
同学可以用前面学习过的wampserver服务器,将文件夹放在服务器下进行访问,json路径写的没有问题。
祝学习愉快!
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星