jQuery get all ids on page

后端 未结 3 811
甜味超标
甜味超标 2021-02-20 08:10

Is it possible to get an array consisting of all id\'s on a page with jQuery?

相关标签:
3条回答
  • 2021-02-20 08:35

    I think this would work

    var array = [];
    $("*").each(function(){
        if(this.id) array.push(this.id);
    });
    
    0 讨论(0)
  • 2021-02-20 08:40

    You could do this:

    var ids = new Array();
    $('[id]').each(function() { //Get elements that have an id=
      ids.push($(this).attr("id")); //add id to array
    });
    //do something with ids array
    

    One note I saw testing this, the FireBug console counts as one, if that's enabled just be aware.

    0 讨论(0)
  • 2021-02-20 08:45
    var ids = $('*[id]').map(function() {
        return this.id;
    }).get();
    

    The .map() method is particularly useful for getting or setting the value of a collection of elements.

    http://api.jquery.com/map/

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