*VS Code中的Ajax

下载插件并使用

下载插件,开放一个端口给要加载的资源,解决跨域问题,没有后端接收数据,用来做小模块很合适

建立文件夹,文件夹下放入jquery插件和json文件

data.json

{"total": 4,"data": [{"name": "三国演义","category": "文学","desc": "一个军阀混战的年代"},{"name": "三国演义2","category": "文学2","desc": "一个军阀混战的年代2"}],"obj": {"adf": "adf"}
}

 将此文件展示到浏览器

ajson.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><script src="jquery-3.6.0.min.js"></script><title>Document</title>
</head>
<body><ul></ul><script>$(function(){$.ajax({type: 'get',url: 'data.json',success:function(xhr){// 获取对象objectxhr=xhr.data;console.log(xhr);str='';for(var i=0;i<xhr.length;i++){console.log(xhr[i].name);console.log(xhr[i].desc);console.log(xhr[i].category);str+=`<li><h3>${xhr[i].name}</h3><p>简介:${xhr[i].desc}<br>类别:${xhr[i].category}</p></li>`}$('ul').append(str);}})})</script>
</body>
</html>

点击右下角的 Go Live 再 右击 -> open with live server或者直接 右击 -> open with live server

可以看到开放了5500端口,报的错不用考虑,刷新一下就消失了

 案例:地图四级制【css样式没有调,可以自行更改】

先点击省份出现所有省,然后逐级递减,为多出的

 取消异步:选取所有信息后才进行提交,也防止ul中的li不能及时加载获取错误的高度

 设置要解析文件的编码格式

beforeSend: function (xhr) {xhr.overrideMimeType('text/plain; charset=GBK'); // 指定GBK编码},
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><script src="jquery-3.6.0.min.js"></script><title>Document</title><style>*{padding: 0;margin: 0;list-style: none;}#china{margin: 0 auto;border: 1px solid red;width: 300px;height: 300px;text-align: center;}#top{width: 100%;height: 20px;font-size: 17px;font-weight: 900;}#bottom{position: relative;width: 100%;height: 270px;overflow: hidden;}span{background-color: aqua;display: block;border: 1px solid;height: 25px;float: left;width: 24%;overflow: hidden;}#bottom>ul{position: relative;height: max-content;top: 0px;}#bottom li{height: 25px;font-size: 15px;font-weight: 300;line-height: 25px;border-bottom: 1px solid gray;}.zone{display: block;}.dis{display: none;}</style>
</head>
<body><div id="china"><div id="top"><span id="pro">省份</span><span id="city">城市</span><span id="zone">区县</span><span id="town">城镇</span></div><div id="bottom"><ul id="show1" class="zone" style="background-color: rgb(241, 207, 196);"><!-- 省地区显示域 --></ul><ul id="show2" class="dis" style="background-color: rgb(189, 251, 230);"><!-- C地区显示域 --></ul><ul id="show3" class="dis" style="background-color: rgb(181, 181, 248);"><!-- Z地区显示域 --></ul><ul id="show4" class="dis" style="background-color: #deffbd;"><!-- T地区显示域 --></ul></div></div><script>$(function(){function ulhight(cls,cdiv){var pdiv=$('#bottom').height();// var cdiv=$('#bottom>#ulwheel').height();var high=pdiv-cdiv; // -100reg=/\d+|-\d+/console.log(pdiv,cdiv,high);$(cls).on("mousewheel", function(event){var de= event.originalEvent.deltaY;tp=$(this).css('top');var tpn=Number(tp.match(reg)[0]);// console.log(tpn);if(de < 0){//为负时远离顶部,top增加if(tpn < 0){$(this).css('top','+=10px');}}else{if(tpn >= high){$(this).css('top','-=10px');}}});}var pro='';var city='';var zone='';var town='';// ajax获取json数据function Pct(cls,pro='',city='',zone=''){$.ajax({type:'get',url:'china.json',dataType: 'text',async: false,//取消异步beforeSend: function (xhr) {xhr.overrideMimeType('text/plain; charset=GBK'); // 指定GBK编码},success:function(xhrl){var str='';if(pro === ''){xhrl=JSON.parse(xhrl);}else if(pro != '' && city === '' && zone === ''){xhrl=JSON.parse(xhrl)[pro];}else if(pro != '' && city != '' && zone === ''){xhrl=JSON.parse(xhrl)[pro][city];}else if(pro != '' && city != '' && zone != ''){xhrl=JSON.parse(xhrl)[pro][city][zone];}if(pro != '' && city != '' && zone != ''){for(i in xhrl){str+=`<li>${xhrl[i]}</li>`} }else{for(i in xhrl){str+=`<li>${i}</li>`}}$(cls).html(str);}})}//中途选错的清况下 封装点击top栏的按钮【省市县镇】function clicktop(ulshow,ulnum,num){//需要span对应的ul,需要展示的ul的编号,需要级别编号【省市县】,var uls=$('ul');for(var i=0;i<uls.length;i++){uls[i].className='dis';}uls[ulnum].className='zone';if(num == 1){$("#city").text('城市');$("#zone").text('区县');$("#town").text('城镇');}else if(num == 2){$("#zone").text('区县');$("#town").text('城镇');}else if(num == 3){$("#town").text('城镇');}cdiv=$(ulshow).height();ulhight(ulshow,cdiv);}$('#pro').click(function(){//点击省份,展示所有省份Pct('#bottom>#show1');clicktop('#show1',0,1);});$('#city').click(function(){//点击span,从展示该省下所有的城市列,后面清空Pct('#show2',pro);clicktop('#show2',1,2);})$('#zone').click(function(){ //展示该省下所有的区县列,后面清空Pct('#show3',pro,city);clicktop('#show3',2,3);})// 中途不会选错,不用点上方spanfunction onebyone(pid,nowid,){}$('#show1').on('click','li',function(){ //选择省份,展示所有的城市名console.log('省:'+$(this).html());pro=$(this).html();// 获取点击到的省份$('#pro').text(pro);// 更改上方省份为具体名称Pct('#show2',pro);//获取该省份下的所有城市// 隐藏省份选项,显示城市列var uls=$('ul');for(var i=0;i<uls.length;i++){uls[i].className='dis';}uls[1].className='zone';// 为城市列设置滚轮cdiv=$('#show2').height();ulhight('#show2',cdiv);});$("#show2").on('click','li',function(){console.log('市'+$(this).html());city=$(this).html();$('#city').text(city);Pct('#show3',pro,city);var uls=$('ul');for(var i=0;i<uls.length;i++){uls[i].className='dis';}uls[2].className='zone';cdiv=$('#show3').height();ulhight('#show3',cdiv);})$("#show3").on('click','li',function(){console.log('市'+$(this).html());zone=$(this).html();$('#zone').text(zone);Pct('#show4',pro,city,zone);var uls=$('ul');for(var i=0;i<uls.length;i++){uls[i].className='dis';}uls[3].className='zone';cdiv=$('#show4').height();ulhight('#show4',cdiv);})$("#show4").on('click','li',function(){console.log('市'+$(this).html());town=$(this).html();$('#town').text(town);console.log('省-',pro,'市-',city,'县-',zone,'镇-',town);})})</script> 
</body>
</html>

可以嵌入到Django中,将json文件放置于static文件夹下,修改两个路径就可以,【有黄色提示可忽略,有强迫症鼠标悬停会报提示】

urls.py

#四级地图path('map/',views.map),

 view.py

def map(request):return render(request,'09.html')

09.html

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><script src="{% static 'jquery-3.6.0.min.js' %}"></script><title>Document</title><style>*{padding: 0;margin: 0;list-style: none;}#china{margin: 0 auto;border: 1px solid red;width: 300px;height: 300px;text-align: center;}#top{width: 100%;height: 20px;font-size: 17px;font-weight: 900;}#bottom{position: relative;width: 100%;height: 270px;overflow: hidden;}span{background-color: aqua;display: block;border: 1px solid;height: 25px;float: left;width: 24%;overflow: hidden;}#bottom>ul{position: relative;height: max-content;top: 0;}#bottom li{height: 25px;font-size: 15px;font-weight: 300;line-height: 25px;border-bottom: 1px solid gray;}.zone{display: block;}.dis{display: none;}</style>
</head>
<body><div id="china"><div id="top"><span id="pro">省份</span><span id="city">城市</span><span id="zone">区县</span><span id="town">城镇</span></div><div id="bottom"><ul id="show1" class="zone" style="background-color: rgb(241, 207, 196);"><!-- 省地区显示域 --></ul><ul id="show2" class="dis" style="background-color: rgb(189, 251, 230);"><!-- C地区显示域 --></ul><ul id="show3" class="dis" style="background-color: rgb(181, 181, 248);"><!-- Z地区显示域 --></ul><ul id="show4" class="dis" style="background-color: #deffbd;"><!-- T地区显示域 --></ul></div></div><script>$(function(){function ulhight(cls,cdiv){var pdiv=$('#bottom').height();// var cdiv=$('#bottom>#ulwheel').height();var high=pdiv-cdiv; // -100reg=/\d+|-\d+/console.log(pdiv,cdiv,high);$(cls).on("mousewheel", function(event){var de= event.originalEvent.deltaY;tp=$(this).css('top');var tpn=Number(tp.match(reg)[0]);// console.log(tpn);if(de < 0){//为负时远离顶部,top增加if(tpn < 0){$(this).css('top','+=10px');}}else{if(tpn >= high){$(this).css('top','-=10px');}}});}var pro='';var city='';var zone='';var town='';// ajax获取json数据function Pct(cls,pro='',city='',zone=''){$.ajax({type:'get',url:'/static/china.json',dataType: 'text',async: false,//取消异步beforeSend: function (xhr) {xhr.overrideMimeType('text/plain; charset=GBK'); // 指定GBK编码},success:function(xhrl){var str='';if(pro === ''){xhrl=JSON.parse(xhrl);}else if(pro !== '' && city === '' && zone === ''){xhrl=JSON.parse(xhrl)[pro];}else if(pro !== '' && city !== '' && zone === ''){xhrl=JSON.parse(xhrl)[pro][city];}else if(pro !== '' && city !== '' && zone !== ''){xhrl=JSON.parse(xhrl)[pro][city][zone];}if(pro !== '' && city !== '' && zone !== ''){for(i in xhrl){str+=`<li>${xhrl[i]}</li>`}}else{for(i in xhrl){str+=`<li>${i}</li>`}}$(cls).html(str);}})}//中途选错的清况下 封装点击top栏的按钮【省市县镇】function clicktop(ulshow,ulnum,num){//需要span对应的ul,需要展示的ul的编号,需要级别编号【省市县】,var uls=$('ul');for(var i=0;i<uls.length;i++){uls[i].className='dis';}uls[ulnum].className='zone';if(num === 1){$("#city").text('城市');$("#zone").text('区县');$("#town").text('城镇');}else if(num === 2){$("#zone").text('区县');$("#town").text('城镇');}else if(num === 3){$("#town").text('城镇');}cdiv=$(ulshow).height();ulhight(ulshow,cdiv);}$('#pro').click(function(){//点击省份,展示所有省份Pct('#bottom>#show1');clicktop('#show1',0,1);});$('#city').click(function(){//点击span,从展示该省下所有的城市列,后面清空Pct('#show2',pro);clicktop('#show2',1,2);})$('#zone').click(function(){ //展示该省下所有的区县列,后面清空Pct('#show3',pro,city);clicktop('#show3',2,3);})// 中途不会选错,不用点上方spanfunction onebyone(pid,nowid,){}$('#show1').on('click','li',function(){ //选择省份,展示所有的城市名console.log('省:'+$(this).html());pro=$(this).html();// 获取点击到的省份$('#pro').text(pro);// 更改上方省份为具体名称Pct('#show2',pro);//获取该省份下的所有城市// 隐藏省份选项,显示城市列var uls=$('ul');for(var i=0;i<uls.length;i++){uls[i].className='dis';}uls[1].className='zone';// 为城市列设置滚轮cdiv=$('#show2').height();ulhight('#show2',cdiv);});$("#show2").on('click','li',function(){console.log('市'+$(this).html());city=$(this).html();$('#city').text(city);Pct('#show3',pro,city);var uls=$('ul');for(var i=0;i<uls.length;i++){uls[i].className='dis';}uls[2].className='zone';cdiv=$('#show3').height();ulhight('#show3',cdiv);})$("#show3").on('click','li',function(){console.log('市'+$(this).html());zone=$(this).html();$('#zone').text(zone);Pct('#show4',pro,city,zone);var uls=$('ul');for(var i=0;i<uls.length;i++){uls[i].className='dis';}uls[3].className='zone';cdiv=$('#show4').height();ulhight('#show4',cdiv);})$("#show4").on('click','li',function(){console.log('市'+$(this).html());town=$(this).html();$('#town').text(town);console.log('省-',pro,'市-',city,'县-',zone,'镇-',town);})})</script>
</body>
</html>

有个问题不知道大家发现没有,top值并不会自动归0

韶关市是广东省最后一个li,滚轮划过来后在点击省份选择北京市,第二栏就上划上去,看不到

 

在clicktop函数下,为获取到的uls设置css('top','0px)

function clicktop(ulshow,ulnum,num){//需要span对应的ul,需要展示的ul的编号,需要级别编号【省市县】,var uls=$('ul');// 添加此行,用以top归0uls.css('top','0px');for(var i=0;i<uls.length;i++){uls[i].className='dis';}uls[ulnum].className='zone';if(num == 1){$("#city").text('城市');$("#zone").text('区县');$("#town").text('城镇');}else if(num == 2){$("#zone").text('区县');$("#town").text('城镇');}else if(num == 3){$("#town").text('城镇');}cdiv=$(ulshow).height();ulhight(ulshow,cdiv);}

地图四级的xlxs文件和json文件【json中的编码格式是GB】

链接:https://pan.baidu.com/s/1BMGf4C5xUiiytWvWp2cVpw?pwd=1g3u 
提取码:1g3u

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/154717.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

POI实现省市级联(二级下拉框)

POI实现省市级联&#xff08;二级下拉框&#xff09; POI级联下拉框 直接上代码测试结果参考文章 POI级联下拉框 业务上经常会用到POI做Excel的导出&#xff0c;有时导出需求比较复杂&#xff0c;这里记录一下自己参考网上大神的水月境的博文完成的一个导出Excel省市级连下拉…

Spark On Hive原理和配置

目录 一、Spark On Hive原理 &#xff08;1&#xff09;为什么要让Spark On Hive&#xff1f; 二、MySQL安装配置&#xff08;root用户&#xff09; &#xff08;1&#xff09;安装MySQL &#xff08;2&#xff09;启动MySQL设置开机启动 &#xff08;3&#xff09;修改MySQL…

计算机网络-应用层(2)

一、DHCP 当需要跨越多个网段提供DHCP 服务时必须使用DHCP 中继代理&#xff0c; 就是在DHCP 客户和服务器之间转发DHCP 消息的主机或路由器。 DHCP 服务端使用UDP 的67号端口来监听和接收客户请求消息&#xff0c; 保留UDP 的68号端口用于接收来自DHCP 服务器的消息回复。 在…

Qt显示中文

中文&#xff1a; unicode&#xff1a;\u4e2d\u6587 utf8&#xff1a;0xE4,0xB8,0xAD,0xE6,0x96,0x87 str 是UI上直接写中文&#xff0c;在这里获取得出的是unicode&#xff1b; str1是得到unicode&#xff0c;相当于fromUtf8() 是将utf8转成unicode&#xff1b; str2是得到…

谈API接入必须了解的各大API调用电商API应用场景

哪些业务场景可以使用API接口&#xff1f; &#xff08;1&#xff09;爬虫业务&#xff1a;在爬虫业务中&#xff0c;使用API接口可以帮助解决IP限制、反爬虫策略等问题&#xff0c;提高爬取数据的效率和稳定性。 &#xff08;2&#xff09;网络安全&#xff1a;在网络安全领…

【用Percona Toolkit给mysql大表在不锁表的情况下建索引】

用Percona Toolkit给mysql大表在不锁表的情况下建索引 安装Percona Toolkit建立索引 安装Percona Toolkit 共分为两步骤&#xff1a; 配置仓库 安装文档1 安装 安装文档2 建立索引 由于mysql8默认使用的是caching_sha2_password&#xff0c;认证方式&#xff0c;而Percona…

图像处理与计算机视觉--神经网络--手动计算

文章目录 1.简单感知器分类模型1.1.简单感知器分类模型介绍1.2.简单感知器分类模型实现 2线性神经元分类模型2.1.线性神经元分类模型介绍2.2.线性神经元分类模型实现 3.基于遍历学习的神经网络计算模型3.1.基于遍历学习的神经网络计算模型介绍3.2.基于遍历学习的神经网络计算模…

代码随想录算法训练营第三十九天丨 动态规划part02

62.不同路径 思路 动态规划 机器人从(0 , 0) 位置出发&#xff0c;到(m - 1, n - 1)终点。 按照动规五部曲来分析&#xff1a; 确定dp数组&#xff08;dp table&#xff09;以及下标的含义 dp[i][j] &#xff1a;表示从&#xff08;0 &#xff0c;0&#xff09;出发&#…

Redux中的异步操作如何处理

聚沙成塔每天进步一点点 ⭐ 专栏简介 前端入门之旅&#xff1a;探索Web开发的奇妙世界 欢迎来到前端入门之旅&#xff01;感兴趣的可以订阅本专栏哦&#xff01;这个专栏是为那些对Web开发感兴趣、刚刚踏入前端领域的朋友们量身打造的。无论你是完全的新手还是有一些基础的开发…

通用管理后台项目笔记 - Vue3+Vite安装环境、封装路由

概述 从0打造通用的后台管理系统&#xff0c;系列的第一篇博客&#xff0c;开发环境NodeYarnVite的开发环境&#xff0c;这是记录的学习笔记。 Node环境 本地使用的是Node v18.18.2&#xff0c;npm v9.8.1,安装脚手架工具&#xff0c;npm、cnpm、yarn3种方式&#xff0c;如果…

吉他、班卓琴和贝斯吉他降分器:Arobas Music Guitar 8.1.1

Arobas Music Guitar 是一款专业的吉他、班卓琴和贝斯吉他降分器。在熟练的手中&#xff0c;它不仅可以让您创作&#xff0c;还可以编辑、聆听和录制&#xff0c;以及导入和导出乐谱。如果有人感兴趣的话&#xff0c;录音是在八个轨道上进行的&#xff0c;你可以为每个轨道单独…

SurfaceFliger与Vsync信号如何建立链接?

Vsync信号上报流程 Vsync的注册函数&#xff0c;来临时会回调HWComposer的hook_VSYNC方法&#xff0c;接着调用到vsync方法中 大致流程梳理&#xff1a; 该方法会通知给SurfaceFliger的onVsyncReceived方法&#xff0c;接着调用DispSync的addResyncSample方法。 DispSyncThr…