利用js将ajax获取到的后台数据动态加载至网页中的方法
作者:sning999 时间:2024-04-16 10:37:03
动态生成二级菜单树:
<script>
jQuery(function($) {
/**********
获取未处理报警信息总数
**************/
var result;
$.ajax({
async:false,
cache:false,
url: "alarm_findPageAlarm.do",//访问后台接口取数据
// dataType : "json",
type: 'POST',
success: function(data){
result = eval('('+ data +')');
}
});
var alarmCount;
alarmCount = result.total;
/**********
静态代码形式
**********/
/*
<li>
<a href="#" rel="external nofollow" class="dropdown-toggle">
<i class="icon-desktop"></i>
<span class="menu-text"> 设备管理 </span>
<b class="arrow icon-angle-down"></b>
</a>
<ul class="submenu">
<li>
<a href="smartTerminal.html" rel="external nofollow" >
<i class="icon-double-angle-right"></i>
智能终端管理
</a>
</li>
<li>
<a href="labelPrinter.html" rel="external nofollow" >
<i class="icon-double-angle-right"></i>
标签打印机管理
</a>
</li>
</ul>
</li>
*/
/*****从后台取出导航栏数据******/
$.ajax({
async:true,
cache:false,
url: "user_getMenuBuf.do",
// dataType : "json",
type: 'POST',
success: function(result){
var result = eval('('+ result +')');
if(result != undefined && result.length > 0){
var firstMenu = [];
var firstHref = [];
var firstIcon = [];
var subMenu = [];
/******一级导航栏数据*******/
for (var i = 0; i < result.length; i++){
firstMenu[i] = result[i].name;
firstHref[i] = result[i].url;
firstIcon[i] = result[i].iconCls;
/*******添加li标签********/
var menuInfo = document.getElementById("menuInfo");
var firstLi = document.createElement("li");//创建新的 li元素
menuInfo.appendChild(firstLi);//将此li元素添加至页面的ul下一级中
firstLi.style.borderBottom = "0px solid #CCEBF8";//设置li下边框样式
/******设置选中li、离开li时li的样式********/
firstLi.onmouseover = function(){
this.style.background = "#23ACFA";
};
/* firstLi.onmouseover = function(){
this.style.background = "#23ACFA";
}; */
firstLi.onmouseout=function(){
this.style.background = "#0477C0";
};
/******添加a标签**********/
var firstALabel = document.createElement("a");
firstALabel.setAttribute("href", firstHref[i]);//js为新添加的a元素动态设置href属性
firstALabel.setAttribute("class", "dropdown-toggle");
//firstALabel.className = "dropdown-toggle";//兼容性好
firstALabel.setAttribute("target", "content");
//firstALabel.style.backgroundImage="url(./img/17.jpg)"
firstALabel.style.background = "#0477C0";//js为新添加的a元素动态设置背景颜色
// background:url(./img/17.jpg);
firstALabel.style.marginLeft = "20px";//js为新添加的a元素动态设置左外边距
firstLi.appendChild(firstALabel);
firstALabel.onmouseover = function(){
this.style.background = "#23ACFA";
};
/* firstALabel.onmouseover = function(){
this.style.background = "#23ACFA";
}; */
firstALabel.onmouseout=function(){
this.style.background = "#0477C0";
};
/*******添加i标签*******/
var firstILavel = document.createElement("i");
firstILavel.setAttribute("class", firstIcon[i]);
firstILavel.style.color = "#F4F8FF";//动态设置i元素的颜色
firstALabel.appendChild(firstILavel);
/*********添加span标签**********/
var firstSpan = document.createElement("span");
firstSpan.className = "menu-text";
firstSpan.innerHTML = firstMenu[i];//js为新添加的span元素动态设置显示内容
firstSpan.style.fontSize = "14.5px";//js为新添加的span元素动态设置显示内容的字体大小
firstSpan.style.color = "#66D2F1";//js为新添加的span元素动态设置显示内容的字体颜色
firstSpan.style.marginLeft = "15px";
firstALabel.appendChild(firstSpan);
if (firstMenu[i] == "报警信息管理"){
var alarmIcon = document.createElement("span");
alarmIcon.className = "badge badge-important";
alarmIcon.innerHTML = alarmCount; //alarmCount为全局变量,且是通过ajax从后台获取到的
firstSpan.appendChild(alarmIcon);
}
if (result[i].children.length > 0){
var secondHref = [];
var secondMenu = [];
var secondIcon = [];
/*******添加b标签********/
var firstBLabel = document.createElement("b");
firstBLabel.className = "arrow icon-angle-down";
firstBLabel.style.color = "white";
firstALabel.appendChild(firstBLabel);
/********添加ul标签************/
var secondUl = document.createElement("ul");
secondUl.setAttribute("class", "submenu");
firstLi.appendChild(secondUl);
for (var j = 0; j < result[i].children.length; j++){
secondHref[j] = result[i].children[j].url;
secondMenu[j] = result[i].children[j].name;
secondIcon[j] = result[i].children[j].iconCls;
/******添加li标签*******/
var secondLi = document.createElement("li");
secondLi.style.background = "#CCEBF8";
secondUl.appendChild(secondLi);
/*******添加a标签*******/
var secondALabel = document.createElement("a");
secondALabel.setAttribute("href", secondHref[j]);
secondALabel.setAttribute("target", "content");
//secondALabel.style.background = "#CCEBF8";
secondLi.appendChild(secondALabel);
/*******添加i标签**********/
var secondILabel = document.createElement("i");
secondILabel.setAttribute("class", "icon-double-angle-right");
secondALabel.appendChild(secondILabel);
/******添加二级导航信息********/
secondALabel.innerHTML = secondMenu[j];
secondALabel.style.fontSize = "15px";
//secondALabel.style.marginLeft = "60px";
}
}
}
}
},
error: function() {
alert("加载菜单失败");
}
});
})
</script>
静态生成菜单树的代码:
生成菜单树的效果:
来源:https://blog.csdn.net/u012724595/article/details/74288435
标签:js,ajax,后台,加载,网页
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python GUI计算器的实现
2021-11-30 01:00:53
![](https://img.aspxhome.com/file/2023/8/87948_0s.png)
turtle的基础使用之python turtle递归绘图
2023-04-25 23:36:08
![](https://img.aspxhome.com/file/2023/1/115601_0s.png)
详解python 拆包可迭代数据如tuple, list
2022-01-08 19:28:43
SQL Server 2000安装图解教程
2009-09-09 19:59:00
![](https://img.aspxhome.com/file/UploadPic/up/2009090920263675.gif)
django和flask哪个值得研究学习
2021-10-02 16:52:46
Python中的命令行参数解析工具之docopt详解
2021-05-15 03:17:41
vue-cli使用stimulsoft.reports.js的详细教程
2024-04-09 10:58:59
![](https://img.aspxhome.com/file/2023/4/139724_0s.jpg)
基于Pygame中Pygame模块的大战外星人实战
2022-08-30 09:22:28
![](https://img.aspxhome.com/file/2023/5/123555_0s.jpg)
如何用CSS实现图像替换链接文本显示并保证链接可点击
2011-03-03 12:37:00
SQL数据库十四种案例介绍
2024-01-14 14:50:42
Pandas中DataFrame常用操作指南
2023-08-31 14:14:41
vue2 利用echarts 单独绘制省份的步骤
2024-04-09 10:45:42
![](https://img.aspxhome.com/file/2023/3/139853_0s.png)
浅谈Node.js之异步流控制
2024-05-02 17:36:23
Python实现我的世界小游戏源代码
2023-07-26 02:09:08
![](https://img.aspxhome.com/file/2023/4/60704_0s.jpg)
js 实现的可折叠留言板(附源码下载)
2024-04-22 13:07:49
python筛选出两个文件中重复行的方法
2021-02-16 12:53:04
ORACLE应用经验(1)
2010-07-31 12:53:00
Python获取单个程序CPU使用情况趋势图
2021-11-19 02:36:59
![](https://img.aspxhome.com/file/2023/4/115074_0s.png)
Vuex的安装、搭建及案例详解
2024-05-29 22:20:22
![](https://img.aspxhome.com/file/2023/0/123180_0s.png)
深入了解Golang的指针用法
2024-05-21 10:22:58
![](https://img.aspxhome.com/file/2023/6/123986_0s.png)