lastChild not working

前端 未结 6 790
予麋鹿
予麋鹿 2021-01-25 01:20

I have the following code:







        
6条回答
  •  生来不讨喜
    2021-01-25 01:50

    The jQuery way:

    // assuming the last child is always a div
    var lastcommentq = $('#div > div:last-child').attr('id');
    
    // alternatively
    var lastcommentq0 = $('#div').children('div').last().attr('id');
    

    The JavaScript way:

    var lastcommentq = document.getElementById('div').lastElementChild.id;
    

    Note that this works in all modern browsers and IE 9+. See lastElementChild on MDN.

提交回复
热议问题