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

前端 未结 8 672
伪装坚强ぢ
伪装坚强ぢ 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:31

    well, since it is a form, im sure that you want to iterate only over the form elements and not all the tags in the document ( like href , div's etc.. )

    for (var i=0; i < form.elements.length; i++) {
       var elementId = form.elements[i].id;
    }
    
    0 讨论(0)
  • 2021-01-31 08:32

    If you're doing your development using a fairly modern browser, you can use querySelectorAll(), then use Array.prototype.forEach to iterate the collection.

    var ids = document.querySelectorAll('[id]');
    
    Array.prototype.forEach.call( ids, function( el, i ) {
        // "el" is your element
        console.log( el.id ); // log the ID
    });
    

    If you want an Array of IDs, then use Array.prototype.map:

    var arr = Array.prototype.map.call( ids, function( el, i ) {
        return el.id;
    });
    
    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2021-01-31 08:36

    The jQuery selector $('[id]') will get all the elements with an id attribute:

    $('[id]').each(function () {
        do_something(this.id);
    });
    

    Working example here: http://jsfiddle.net/RichieHindle/yzMjJ/2/

    0 讨论(0)
  • 2021-01-31 08:36

    with jQuery

    $('*').map(function() {
       return this.id || null;
    }).get().join(',');
    

    this gets all the elements in the DOM, and runs a function on each to return the id (and if undefined, returning null won't return anything. This returns a jQuery object which is then converted to a JavaScript array with get() and this is then converted to a comma-separated string of ids.

    Try it on this page and you get

    "notify-container,overlay-header,custom-header,header,portalLink,topbar,hlinks,hlinks-user,hlinks-nav,hlinks-custom,hsearch,search,hlogo,hmenus,nav-questions,nav-tags,nav-users,nav-badges,nav-unanswered,nav-askquestion,content,question-header,mainbar,question,edit-tags,link-post-7115022,close-question-7115022,flag-post-7115022,comments-7115022,add-comment-7115022,comments-link-7115022,answers,answers-header,tabs,answer-7115033,link-post-7115033,flag-post-7115033,comments-7115033,add-comment-7115033,comments-link-7115033,answer-7115042,link-post-7115042,flag-post-7115042,comments-7115042,add-comment-7115042,comments-link-7115042,answer-7115043,link-post-7115043,delete-post-7115043,flag-post-7115043,post-editor-7115043,wmd-button-bar-7115043,wmd-button-row-7115043,wmd-bold-button-7115043,wmd-italic-button-7115043,wmd-spacer1-7115043,wmd-link-button-7115043,wmd-quote-button-7115043,wmd-code-button-7115043,wmd-image-button-7115043,wmd-spacer2-7115043,wmd-olist-button-7115043,wmd-ulist-button-7115043,wmd-heading-button-7115043,wmd-hr-button-7115043,wmd-spacer3-7115043,wmd-undo-button-7115043,wmd-redo-button-7115043,wmd-help-button-7115043,wmd-input-7115043,draft-saved-7115043,communitymode-7115043,wmd-preview-7115043,fkey,author,edit-comment-7115043,edit-comment-error-7115043,submit-button-7115043,comments-7115043,add-comment-7115043,comments-link-7115043,post-form,post-editor,wmd-button-bar,wmd-input,draft-saved,communitymode,wmd-preview,fkey,author,submit-button,show-editor-button,sidebar,qinfo,adzerk2,newsletter-ad,newsletter-ad-header,newsletter-signup-container,newsletter-signup,newsletter-preview-container,newsletter-preview,h-related,feed-link,feed-link-text,prettify-lang,footer,footer-menu,footer-sites,footer-flair,svnrev,copyright"

    0 讨论(0)
  • 2021-01-31 08:39

    A simple ES6 (es2015) solution based on answer of user113716

    const elementsById = document.querySelectorAll('[id]');
    const elementsByIdArr = Array.prototype.map.call(elementsById, el => el.id);
    

    /**
    * identify all element ID's on a page,
    * construct array of all element ID's on a page,
    */
    
    const elementsById = document.querySelectorAll('[id]');
    const elementsByIdArr = Array.prototype.map.call(elementsById, el => el.id);
    
    for (const el of elementsByIdArr) {
      document.getElementById(el).innerHTML = `My id is &quot;&num;${el}&quot;`;
    }
    .title {font-weight: bolder; font-size: 24px;}
    .description {line-height: 1.8em;}
    <div id="primary" class="title"></div>
    <div id="description" class="description"></div>

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