Home  >  Article  >  Web Front-end  >  JavaScript DOM operation forms and styles_Basic knowledge

JavaScript DOM operation forms and styles_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:04:201006browse

1 Operation form

f5d188ed2c074f8b944552db028f98a1 tag is the most complex structure in HTML. We can create and generate it through DOM, or operate it through HTMLDOM;

// 使用DOM来创建表格;
 var table = document.createElement('table');
 table.border = 1;
 table.width = 300;

 var caption = document.createElement('caption');
 table.appendChild(caption);
 caption.appendChild(document.createTextNode('人员表'));

 var thead = document.createElement('thead');
 table.appendChild(thead);

 var tr = document.createElement('tr');
 thead.appendChild(tr);

 var th1 = document.createElement('th');
 var th2 = document.createElement('th');

 tr.appendChild(th1);
 th1.appendChild(document.createTextNode('姓名'));
 tr.appendChild(th2);
 th2.appendChild(document.createTextNode('年龄'));

 document.body.appendChild(table);

// The table is complex and has many levels. It will be troublesome to use the previous DOM to obtain a certain element; it is recommended to use HTMLDOM;
Introduction to HTMLDOM properties and methods
Property or method Description
caption holds a reference to the 63bd76834ec05ac1f4c0ebbeaafb0994 element;
tBodies holds an HTMLCollection of 92cee25da80fac49f6fb6eec5fd2c22a elements;
tFoot holds a reference to the 06669983c3badb677f993a8c29d18845 element;
tHead holds a reference to the ae20bdd317918ca68efdc799512a9b39 element;
rows holds an HTMLCollection of a34de1251f0d9fe1e645927f19a896e8 elements;
createTHead() creates the ae20bdd317918ca68efdc799512a9b39 element and returns the reference;
createTFoot() creates the 06669983c3badb677f993a8c29d18845 element and returns the reference;
createCpation() creates the 63bd76834ec05ac1f4c0ebbeaafb0994 element and returns the reference;
deleteTHead() deletes ae20bdd317918ca68efdc799512a9b39 element;
deleteTFoot() deletes the 06669983c3badb677f993a8c29d18845 element;
deleteCaption() deletes the 63bd76834ec05ac1f4c0ebbeaafb0994 element;
deleteRow(pos) deletes the specified row;
insertRow(pos) inserts a row into the specified position in the rows collection;

Attributes and methods added by the92cee25da80fac49f6fb6eec5fd2c22a element
rows holds an HTMLCollection of rows in the 92cee25da80fac49f6fb6eec5fd2c22a element;
deleteRow(pos) deletes the row at the specified position;
insertRow(pos) inserts a row into the specified position in the rows collection;

a34de1251f0d9fe1e645927f19a896e8Attributes and methods added to the element
cells holds the HTMLCollection collection of cells in the a34de1251f0d9fe1e645927f19a896e8 element;
deleteCell(pos) deletes the cell at the specified position;
insertCell(pos) inserts a cell into the specified position of the cells collection and returns a reference;

//HTMLDOM gets the 63bd76834ec05ac1f4c0ebbeaafb0994
of the table alert(table.caption.innerHTML); // Get the content of the caption;

// PS: ae20bdd317918ca68efdc799512a9b39 and 06669983c3badb677f993a8c29d18845 are unique in a table, there can only be one;
// And 92cee25da80fac49f6fb6eec5fd2c22a is not unique, it can be multiple, so that the finally returned ae20bdd317918ca68efdc799512a9b39 and 06669983c3badb677f993a8c29d18845 are element references; and 92cee25da80fac49f6fb6eec5fd2c22a is a collection of elements;

2 Operation Style

As an auxiliary to (X)HTML, CSS can enhance the display effect of the page, but not every browser can support the latest CSS capabilities;
CSS capabilities are closely related to DOM levels, so it is necessary to detect the level of CSS capabilities supported by the current browser;
There are three ways to define styles in HTML:
(1). Use the style attribute to define styles for specific elements;
(2). Use the e5323b80b7cdba5fa628a1c89784fa75 element to define embedded styles;
(3). Contain external style sheet files through the c88766e8e98f17593dff2d30c01c03cf element; 1 // DOM1 level implements the most basic document processing, DOM2 and DOM3 add more interactive capabilities on this basis;
DOM2 adds CSS programming access methods and changing CSS style information;
Detect whether the browser supports DOM1-level CSS capabilities or DOM2-level CSS capabilities
​​​ alert('DOM1 level CSS capability:' document.implementation.hasFeature('CSS','2.0'));
alert('DOM2 level CSS capability:' document.implementation.hasFeature('CSS2','2.0'));

1. Access the style of the element

(1).style property/object
// Any HTML element tag will have a common attribute: style, which will return a CSSStyleDeclaration object;
CSS properties and JavaScript calls
CSS properties JavaScript calls
color style.color
font-size style.fontSize
float style.cssFloat (non-IE)
float style.styleFloat(IE)
var box = document.getElementById('box');
box.style; // CSSStyleDecaration;
box.style.color; box.style.fontSze; // 20px;
// Compatible with IE’s float operation;
Typeof box.style.cssFloat != 'undefined' ? box.style.cssFloat = 'right' : box.style.styleFloat = 'right';

    DOM2级样式规范为style对象定义属性和方法
属性或方法                       说明
cssText                 访问或设置style中的CSS代码;
    box.style.cssText;                      // 获取CSS代码;
    // PS:style属性仅仅只能获取行内的CSS样式,对于另外两种形式内联c9ccee2e6ea535a969eb3f532ad9fe89和链接2cdf5bf648cf2f33323966d7f58a7f3f方法则无法获取到;

(2).计算后的样式:getComputedStyle/currentStyle
// 虽然可以通过style来获取单一值的CSS样式,但对于复合值的样式信息,就需要通过计算样式来获取;
// DOM2级样式,window对象下提供了getComputedStyle()方法:接收两个参数,需要计算的样式元素,和伪类(:hover);如果没有伪类,则null;
// getComputedStyle()方法返回一个CSSStyleDeclaration对象;(与style属性的类型相同);其中包含当前元素的所有计算的样式;
// PS:IE不支持这个DOM2级的方法,但有个类似的属性可以使用currentStyle属性;
var box = document.getElementById('box');
var style = window.getComputedStyle ? window.getComputedStyle(box,null) : null ||box.currentStyle;
alert(style.color); // 颜色在不同的浏览器会有不同rgb()格式;
alert(style.border); // 不同浏览器不同的结果;
alert(sytle.fontFamily); // 计算显示复合的样式值;
// PS:border属性是一个综合属性,所以它在Chrome显示了,Firefox为空,IE为undefined;
// 所以,DOM在获取CSS的时候,最好采用完整写法兼容性最好;比如:border-top-color;

2.操作样式表

// 使用style属性可以设置行内的CSS样式,而通过id和class调用是最常用的方法;
  box.className = 'red';         // 通过className关键字来设置样式;
// 添加多个className函数:
  // 判断是否存在这个class;
  function hasClass(element,className){
    return element.className.match(new RegExp('(\\s|^)'+className+'(\\s|$)'));
  }
  // 添加一个class,如果不存在的话;
  function addClass(element,className){
    if(!hasClass(element,className)){
      element.className += " "+className;
    }
  }
  // 删除一个class,如果存在的话;
  function removeClass(element,className){
    if(hasClass(element,className)){
      element.className = element.className.replace(new RegExp('(\\s|^)'+className+'(\\s|$)'),'');
    }
  }
// 之前使用style属性,仅仅只能获取和设置行内的样式;
// 然后学习的getComputedStyle和currentStyle,只能获取却不能设置;

(1).获得CSSStyleSheet
// CSSStyleSheet类型可以通过2cdf5bf648cf2f33323966d7f58a7f3f元素和c9ccee2e6ea535a969eb3f532ad9fe89元素包含的样式表;
document.implementation.hasFeature('StyleSheet','2.0'); // 是否支持DOM2级样式表;
document.getElementsByTagName('link')[0]; // HTMLLinkElement;
document.getElementsByTagName('style')[0]; // HTMLStyleElement;
// 这两个元素本身返回的是HTMLLinkElement和HTMLStyleElement类型;但CSSStyleSheet类型更加通用一些;
// 得到这个类型非IE使用sheet属性,IE使用styleSheet;
var link = document.getElementsByTagName('link')[0];
var sheet = link.sheet || link.styleSheet; // 得到CSSStyleSheet;

(2).CSSStyleSheet对象的属性和方法
CSSStyleSheet属性或方法
属性或方法 说明
disabled 获取和设置样式表是否被禁用;
href 如果是通过2cdf5bf648cf2f33323966d7f58a7f3f包含的,则样式表为URL,否则为null;
media 样式表支持的所有媒体类型的集合;
ownerNode 指向拥有当前样式表的指针;
parentStyleSheet @import导入的情况下,得到父CSS对象;
title ownerNode中title属性的值;
type 样式表类型字符串;
cssRules 样式表包含样式规则的集合,IE不支持; IE为rules;12 ownerRule @import导入的情况下,指向表示导入的规则,IE不支持;
deleteRule(index) 删除cssRules集合中指定位置的规则,IE不支持;
insertRule(rule,index) 向cssRules集合中指定位置插入rule字符串,IE不支持;

sheet.disabled; // false; 可设置为true;
sheet.href; // css的URL;
sheet.media; // MediaList,集合;
sheet.title; // 得到title属性的值;
sheet.cssRules; // CSSRuleList,样式表规则集合;
sheet.deleteRule(0); // 删除第一个样式规则;
sheet.insertRule("body {background-color:red}",0); // 在第一个位置添加一个样式规则;

// PS:IE中不支持的属性和方法,IE有替代版本;
sheet.rules; // 代替cssRules的IE版本;
sheet.removeRule(0); // 代替deleteRule的IE版本;
sheet.addRule("body","{background-color:red",0); // 代替insertRule的IE版本;

// 除了刚才的方法可以得到CSSStyleSheet类型,还有一种方法是通过document的styleSheets属性来获取;
document.styleSheets; // StyleSheetList,集合;
var sheet = docuemnt.styleSheets[0]; // CSSStyleSheet,第一个样式表对象;

// 添加CSS规则,并兼容所有浏览器函数;
  var sheet = docuemnt.styleSheets[0];
  insertRule(sheet,"body","background-color:red;",0);
  function insertRule(sheet,selectorText,cssText,postion){
    // 如果是非IE;
    if(sheet.insertRule){
      sheet.insertRule(selectorText+"{"+cssText+"}",postion);
    // 如果是IE
    }else if(sheet.addRule){
      sheet.addRule(selectorText,cssText,postion);
    }
  }

(3).CSSRules style sheet rule collection list;
// Through the CSSRules attribute (non-IE) and rules attribute (IE), we can get the rule collection list of the style sheet;
// This way we can perform specific operations on each style;
var sheet = docuemnt.styleSheets[0]; // CSSStyleSheet;
var rules = sheet.cssRules || sheet.rules; // CSSRuleList, a collection list of rules for style sheets;
var rule = rules[0]; // CSSStyleRule, the first rule of the style sheet;
Properties of CSSRules
Property Description
cssText gets the text corresponding to the current overall rule, IE does not support it;
parentRule @import imported, returns rule or null, IE does not support it;
parentStyleSheet The style sheet of the current rule, IE does not support it;
selectorText gets the selector text of the current rule;
style returns a CSSStyleDeclaration object, which can get and set styles;
type represents the constant value of the rule. For style rules, the value is 1, which is not supported by IE;

rule.cssText; // Style text of the current rule;
rule.selectorText; // #box; style selector;
rule.style.color; // red; get the specific style value;
rule.style.backgroundColor = "green"; // Modify the style information of a certain rule;

3 Summary

The DOM2-level style module is mainly developed for operating the style information of elements. Its characteristics are as follows:
(1). Each element has an associated style object, which can be used to determine and modify the in-line style;
(2). To determine the calculated style of an element (including all CSS rules applied to it), you can use the getComputedStyle() method;
(3).IE supports similar methods currentStyle();
(4). Style sheets can be accessed through the document.styleSheets collection; 6 // (5). Style sheet rule collection list CSSRules; 1 // Three methods of operating CSS:


The first style is inline, readable and writable;
The second type of inline/inline and link uses getComputedStyle or currentStyle, which is readable but not writable;
The third type of inlining and connection uses cssRules or rules, which is readable and writable;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn