How do I enumerate all of the html id's in a document with javascript?

前端 未结 8 873
伪装坚强ぢ
伪装坚强ぢ 2021-01-31 07:54

I would like to be able to use javascript to find every id (or name) for every object in an html document so that they can be printed at the bottom of the page.

To under

8条回答
  •  忘掉有多难
    2021-01-31 08:35

    Get all tags with the wildcard:

    var allElements = document.getElementsByTagName('*');
    for(var i = 0; i < allElements.length; i++) {
        // ensure the element has an Id that is not empty and does exist
        // and string other than empty, '', will return true
        allElements[i].id && console.log(allElements[i].id);
    }
    

提交回复
热议问题