jQuery each error :Uncaught TypeError: Cannot use 'in' operator to search for '18' in div[data-role=page]

前端 未结 3 1129
离开以前
离开以前 2021-01-18 21:46

My html is like

...
3条回答
  •  既然无缘
    2021-01-18 22:20

    You need to supply a jQuery collection, instead of just a selector to the $.each():

    $.each($("div[data-role=page]"), function (){
        console.log(this.id);
    });
    

    Or, even better:

    $("div[data-role=page]").each(function (){
        console.log(this.id);
    });
    

    Please note that I replaced $(this).attr('id') with this.id. It gets exactly the same property, but it's way more efficient.

    Fiddle Example

提交回复
热议问题