Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in dom, what is functional difference between an html element and a fragment?

have found fragments to be very useful in constructing dynamic html - like using a bag of html elements and the bag itself dissolving when put into place - very nice.

I had assumed the methods for html elements and fragments were the same, but I think this is incorrect.

it seems that fragments have node methods - appendChild() etc - but do not have more sophisticated html element methods, for example, getElementsByTagName().

is this assessment correct? or am I shooting myself in the foot in a new way?

like image 845
cc young Avatar asked Nov 30 '25 14:11

cc young


2 Answers

A DocumentFragment extends the Node interface and is defined as,

interface DocumentFragment : Node {
};

An HTMLElement extends from Element, and its interface is defined as,

interface HTMLElement : Element {
    attribute DOMString id;
    attribute DOMString title;
    attribute DOMString lang;
    attribute DOMString dir;
    attribute DOMString className;
};

Element in-turn extends the Node interface. It contains the method getElementsByTagName among several others,

interface Element : Node {
    ...
    NodeList getElementsByTagName(in DOMString name);
    ..
};

So to answer your question in short, a document fragment and an html element both share the node interface.

like image 115
Anurag Avatar answered Dec 03 '25 03:12

Anurag


The DocumentFragment interface extends the Node interface, thus providing methods to insert and clone elements, but not getElementsByTagName() and the like. These are to be found in the Document interface (which also extends Node).

like image 35
jensgram Avatar answered Dec 03 '25 02:12

jensgram



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!