Home > front end >  DOM and BOM
DOM and BOM

Time:03-20


the document is the root of the DOM, is the whole page;
Windows are roots in the BOM, it is the entire browser;
BOM contains the DOM;


BOM
location
 location. Reload ()//refresh the page 
location.href=https://bbs.csdn.net/topics/url//has a history of jump
Location. The assign=url/jump/copy, recorded history
The location. The replace=url/jump/replace, have no history record
The location. Serach//obtain address bar? Behind the string
Location. The hash//# for the address bar at the back of the content (for the name of the anchor)
The location. The hostname//host domain name
The location. The port//port
The location, the pathname//path
The location. The protocol//agreement


history
 
History. The back ()//jump backward step
History. The forward ()//step jump forward
History. Go (0)//0 is to refresh the page, backward step jump jump one step forward, - 1


the navigator userAgent
Returned from the client sends the server the user-agent header values;



document
 document. The documentElement//HTML tags 
Document. The body//body tag
Document. The head//head tag
Document. The title//titile tag
Document. The URL//the current URL
Document. The domain//the current domain name



//create 
Document. The createElement method (' tags')//through the tags to create the DOM
Document. CreateDocumentFragment ()//create debris collector
Document. The createTextNode (' text ')//create a text node

//get
//to get to the NodeList list
Document. GetElementsByName (name)//by name to get element list
Document. The getElementsByClassName (class)//by class to obtain element list
Document. The getElementsByTagName (tag)//element accessed by the tag list
Document. QuerySelectorAll (selector)//by selector to obtain a list element
//get the only element
Document. The getElementById (id)//by id for element
Document. QuerySelector (selector)//by selector to obtain corresponding to elements in the list of the first element

//insert
The parent container. The appendChild (child)//insert element to the parent container
The parent container. The insertBefore (new elements, insert in front of the who)//insert element to the parent container in the front of the target element
//ps: insert element is moving elements will not copy the element

//copy
The target element. CloneNode (false/true);//shallow copy (default)/deep copy
//copies elements into a new content, the node can be reproduced; Deep copy is the element, the content and child elements together to copy, shallow copy is just copying the element itself
//ps: when you copy, if you have any id tags, copies of id must do change

//delete
Want to delete the element. The remove ()//delete elements
The parent container. RemoveChild (child)//by the parent container delete child elements, can only delete child element, element cannot be deleted offspring;
//ps: delete the element is removed from the page only, but still stored in the heap; Elements=null, can completely remove elements

//replace
The parent container. The replaceChild (new elements, it is necessary to replace the old elements)



But the DOM are objects, so the DOM can set up a series of object properties and methods;
Some attributes are DOM initial set of properties (system Settings properties);
DOM attributes are divided into two kinds: tag attributes and object attribute
Object properties: we can use js API to obtain the DOM object, the DOM object is the object attribute
 const div=document. QuerySelector (' div ') 
Div. The className

Tag attributes: we set on in the HTML tag is the tag attribute;
 & lt; Div & gt; On the label attribute class

Whether object properties or attributes, we can be custom, but the custom properties of the object cannot be set on the label, the custom tag attributes cannot be in the DOM directly obtained by object attribute

set the label attribute to add and delete
 
Const div=docuemnt. QuerySelector (' div ');
Div. SetAttribute (key, value)//elements add key="value" on the label tag attribute
Div. GetAttribute (key)//get the element tag attributes key value: the value
Div. RemoveAttribute (key)//delete the element tag attributes key



 
Const div=document. QuerySelector (' div ');
//style Settings
Object. The assign (div. Style, {
Width: 100 px,
Height: "100 px"
})
//any CSS style cannot by div. The style object access, this just set the inline style; Load HTML after began to implement CSS;

//CSS styles for compatible writing
Var bgColor;
Try {
//Google
BgColor=getComputedStyle (div). BackgroundColor;
{} the catch (err)
BgColor=div. CurrentStyle. BackgroundColor;
}
  • Related