JavaScript項目總結一

來源:互聯網
上載者:User

標籤:字串   indexof   多個   父類   style   ssr   ima   unique   head   

1.類選擇其下,第一個

$(‘selector‘).first()==$(‘selector:first‘)==$(‘selector:eq(0)‘)

2.如果要選擇非第一個

$(‘selector:eq(n)‘)

3.Dom對象添加事件(比較隨意)和JQuery 對象添加事件不一樣(建議用on)

Dom.onClick=function(e){}$(‘selector‘).on(‘click‘,function(){})

4.DOM 遍曆是昂貴的,所以盡量將會重用的元素緩衝

$this=$(this)

5.使用 jQuery 中的方法缺少經驗,需要查看的文檔,有一個更好或更快的方法

$(‘#id‘).data(key,value)//改成(高效)$.data(‘#id‘,key,value)

6.建議申明的全劇變數和調用時採用

window.global

7.滑鼠點擊其他地方收合“下拉框”

$(‘body‘).click(function(e) {  if (e.target.id != ‘regionLabel‘ + pageIndex + ‘‘&& e.target.id != ‘regionSelectBox‘ + pageIndex+ ‘‘)   {    if (!$(‘#regionSelectBox‘ + pageIndex + ‘‘).hasClass(‘hide‘))     {      $(‘#regionSelectBox‘ + pageIndex + ‘‘).addClass(‘hide‘);      $(‘#toggleRegionSelectBox‘ + pageIndex + ‘‘).removeClass(‘invert‘);    }  }});

8. toggleClass() 對設定或移除被選元素的一個或多個類進行切換。

該方法檢查每個元素中指定的類。如果不存在則添加類,如果已設定則刪除之。這就是所謂的轉場效果。

9. 同輩.siblings()

$(this).addClass(‘ant-btn-primary‘).siblings().removeClass(‘ant-btn-primary‘);

10.去空

$(this).text().trim();

11.函數結束時緊跟的一對圓括弧說明這個函數定以後立即執行

12.在JQuery中,先去空,利用$(this).text().trim();接著進行if判斷時,最好把==換成indexOF模糊比對

13.檢測CSS類,hasClass只能接受單個類名作為參數,並且不支援函數參數,is()方法更靈活,可以用來做同樣的事。

$(‘p‘).hasClass(‘className‘)$(‘#id‘).is(‘.className‘)$(‘#id‘).is(‘.classOne.classTwo‘)

14.數組去重

Array.prototype.unique=function(){    var result=[];    var json={};    for (var i = 0; i < this.length; i++) {        if (!json[this[i]]) {            result.push(this[i]);            json[this[i]]=1;        }    }    return result;};//調用arr.unique();

15.防止點擊事件冒泡(冒泡到祖、父類,因為它們可能也有點擊事件)


//下面的方法可以借鑒:使用者組成動態產生Table,特別是join() 方法用於把數組中的所有元素放入一個字串。arrayObject.join(separator)

initE2XNeighborTable: function(){               var $table = $(‘#tableEx2Neighbor‘).empty();              var ring = $(‘#configAccessRing‘).val();              var list = NEIGHBOR_INFO[ring];              var thead = [                      ‘<tr>‘,                      ‘<th>pCell</th>‘,                      ‘<th>sCell</th>‘,                      ‘<th>No. of pCell Edge Users</th>‘,                      ‘<th>No. of Scell Edge Users</th>‘,                      ‘<th>No. of Neighboring Cells</th>‘,                      ‘</tr>‘                      ].join(‘‘);              var tbody = ‘‘;              $.each(list, function(i, row){                 tbody += [                      ‘<tr>‘,                      ‘<td>‘ + row.pCell + ‘</td>‘,                      ‘<td>‘ + row.sCell + ‘</td>‘,                      ‘<td>‘ + row.pCellUser + ‘</td>‘,                      ‘<td>‘ + row.sCellUser + ‘</td>‘,                      ‘<td>‘ + row.neighborNum + ‘</td>‘,                      ‘</tr>‘                      ].join(‘‘);                   });              $table.append(thead + tbody);              },

 

JavaScript項目總結一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.