国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

利用js將ajax獲取到的后臺數(shù)據(jù)動態(tài)加載至網(wǎng)頁中的方法

2019-11-19 13:19:28
字體:
供稿:網(wǎng)友

動態(tài)生成二級菜單樹:

<script>jQuery(function($) {/**********獲取未處理報警信息總數(shù)**************/var result;$.ajax({async:false,cache:false,url: "alarm_findPageAlarm.do",//訪問后臺接口取數(shù)據(jù)// dataType : "json",type: 'POST',success: function(data){result = eval('('+ data +')');}});var alarmCount;alarmCount = result.total;/**********靜態(tài)代碼形式**********//* <li><a href="#" rel="external nofollow" class="dropdown-toggle"><i class="icon-desktop"></i><span class="menu-text"> 設(shè)備管理 </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>*//*****從后臺取出導航欄數(shù)據(jù)******/$.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 = [];/******一級導航欄數(shù)據(jù)*******/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");//創(chuàng)建新的 li元素menuInfo.appendChild(firstLi);//將此li元素添加至頁面的ul下一級中firstLi.style.borderBottom = "0px solid #CCEBF8";//設(shè)置li下邊框樣式/******設(shè)置選中l(wèi)i、離開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元素動態(tài)設(shè)置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元素動態(tài)設(shè)置背景顏色// background:url(./img/17.jpg);firstALabel.style.marginLeft = "20px";//js為新添加的a元素動態(tài)設(shè)置左外邊距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";//動態(tài)設(shè)置i元素的顏色firstALabel.appendChild(firstILavel);/*********添加span標簽**********/var firstSpan = document.createElement("span");firstSpan.className = "menu-text";firstSpan.innerHTML = firstMenu[i];//js為新添加的span元素動態(tài)設(shè)置顯示內(nèi)容firstSpan.style.fontSize = "14.5px";//js為新添加的span元素動態(tài)設(shè)置顯示內(nèi)容的字體大小firstSpan.style.color = "#66D2F1";//js為新添加的span元素動態(tài)設(shè)置顯示內(nèi)容的字體顏色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>

靜態(tài)生成菜單樹的代碼:

生成菜單樹的效果:

以上這篇利用js將ajax獲取到的后臺數(shù)據(jù)動態(tài)加載至網(wǎng)頁中的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 新密市| 巴林右旗| 瑞金市| 梅州市| 黄平县| 正宁县| 全椒县| 南通市| 德昌县| 海晏县| 莲花县| 凌源市| 襄汾县| 苗栗县| 镇坪县| 松江区| 额济纳旗| 津南区| 西昌市| 江山市| 兴业县| 女性| 大理市| 石楼县| 呼伦贝尔市| 乐陵市| 色达县| 修文县| 博爱县| 江门市| 嘉荫县| 论坛| 桂阳县| 固阳县| 长泰县| 运城市| 巴彦淖尔市| 寿光市| 泰兴市| 且末县| 额敏县|