jQuery簡單操作,jquery操作
jQuery簡單操作
一:簡介
在前端方面一直很小白、只瞭解一些基本的JS、還不是太熟悉、jQuery也是、用的時候有時候很簡單的用法還要去查、系統的學習一下、把常用的記錄一下做個備忘、也是筆記。不斷更新。。。
二:常用函數
1、$(‘#id’).remove()——移除id為id的元素。
2、$(‘#id’).empty()—— 移除id為id的元素中的內容。
三:實用外掛程式
1、table中合并相同內容的單元格:
a) 內容:
//封裝的一個JQuery小外掛程式、實現相同內容合併儲存格的代碼jQuery.fn.rowspan = function(colIdx) {return this.each(function() {var that;$('tr', this).each(function(row) {$('td:eq(' + colIdx + ')', this).filter(':visible').each(function(col) {if (that != null&& $(this).html() == $(that).html()) {rowspan = $(that).attr("rowSpan");if (rowspan == undefined) {$(that).attr("rowSpan", 1);rowspan = $(that).attr("rowSpan");}rowspan = Number(rowspan) + 1;$(that).attr("rowSpan", rowspan);$(this).hide();} else {that = this;}});});});}
b) 測試內容:動態產生資料table、頁面和js:
<body onload="init();"><div align="center"><table id="logDetailTable" width="99%" border="1" collapse="collapse"><thead><tr><th width="20%" height="40px">項目名稱</th><th width="20%" height="40px">監控類型</th><th width="35%" height="40px">子項目名</th><th width="25%" height="40px">日誌資訊</th></tr></thead><tbody id="logDetailInfo"></tbody></table></div></body>
function init(){//擷取資料集result...//構建table:for (var i in result){fillShowLogTable(result[i]);}//指定要合并的table的那些行$('#logDetailTable').rowspan(0);$('#logDetailTable').rowspan(1);}function fillShowLogTable(list){var str = '';str += '<tr><td style="width:20%;text-align:center">'+list.PRO_TITLE+'</td>';str += '<td style="width:20%;height:30px;text-align:center">'+list.SUB_PROJECT_NAME+'</td>';str += '<td style="width:35%;height:30px;text-align:center">'+list.TASK_NAME+'</td>';if(list.TIP_VALUE == 'no'){list.TIP_VALUE = '無新日誌';str += '<td class="noNewLog" style="width:25%;height:30px;text-align:center">'+list.TIP_VALUE+'</td></tr>';}else if(list.TIP_VALUE == 'yes'){list.TIP_VALUE = '有新日誌';str += '<td class="hasNewLog" style="width:25%;height:30px;text-align:center">'+list.TIP_VALUE+'</td></tr>';}$(str).appendTo($('#logDetailInfo'));}
c) 測試效果:
總結:
學以致用。實踐是最快的捷徑!