Count number of img tags inside a div tag

前端 未结 10 1947
暗喜
暗喜 2021-02-04 05:29

My code goes like this.

相关标签:
10条回答
  • 2021-02-04 05:38

    Use

    var count = $("#some_id").find('img').length;
    
    0 讨论(0)
  • 2021-02-04 05:38

    Or the plain version without jQuery:

    document.getElementById("some_id").getElementsByTagName("img").length
    
    0 讨论(0)
  • 2021-02-04 05:46
    var count = $("#some_id img").length; 
    

    It will give you total length of images inside a div.

    0 讨论(0)
  • 2021-02-04 05:47

    Try this

    var count = $('#some_id').find('img').length;
    
    0 讨论(0)
  • 2021-02-04 05:48

    Count img inside #some_div:

     $("#some_id img").length
    

    If you want only the direct children, not all descendants:

    $("#some_id > img").length
    
    0 讨论(0)
  • 2021-02-04 05:51
    var count = $("#some_id img").length
    

    Select the image tags like this.

    0 讨论(0)
提交回复
热议问题