سؤال

I have been testing for hours different codes for traveling the DOM.

$(element).children('.classelement').each(function(){
    alert(this) // returns "object HtmlDivElement"
    alert(this.lastChild) // returns "object Text"

    // NOTE: The last child is a <a> element
});

Why this.lastChild returns object Text?. If this returns a DOM object, why lastChild doesn't?

And after i have the next question: What kind of object returns a this inside a .each?

What i must to do for get an DOM object in this case?

هل كانت مفيدة؟

المحلول

lastChild returns the last node any of type in an element (including space, tab or newline written in the file). If you need the last HTMLElement within an element, you have to use lastElementChild.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top