dom

dom.js

Members

static, constant $

用匹配选择器获取单个DOM元素,依赖于其他DOM的 context,默认为 document

static, constant $$

获取所有匹配选择器的元素

Methods

static addClass(element, classToAdd) → {Element}

给元素加对应class

Parameters:
Name Type Description
element Element

Element to add class name to.

classToAdd string

Class name to add.

Returns:
Element -

The dom element with the added class name.

static appendContent(el, content) → {Element}

格式化文本并添加到元素

Parameters:
Name Type Description
el Element

Element to append normalized content to.

content String | Element | TextNode | Array | function

See the content argument of dom:normalizeContent

Returns:
Element -

The element with appended normalized content.

static blockTextSelection()

拖动控件时阻止选择文本

static createEl(tagNameopt, propertiesopt, attributesopt, contentopt) → {Element}

用所给属性创建元素

Parameters:
Name Type Attributes Default Description
tagName string <optional>
'div'

Name of tag to be created.

properties Object <optional>
{}

Element properties to be applied.

attributes Object <optional>
{}

Element attributes to be applied.

content String | Element | TextNode | Array | function <optional>

Contents for the element (see: dom:normalizeContent)

Returns:
Element -

The element that was created.

static emptyEl(el) → {Element}

清空元素内容

Parameters:
Name Type Description
el Element

The element to empty children from

Returns:
Element -

The element with no children

static findPosition(el) → {module:dom~Position}

Offset Left.
getBoundingClientRect technique from
John Resig

Parameters:
Name Type Description
el Element

Element from which to get offset

Returns:
module:dom~Position -

The position of the element that was passed in.

See:

static getAttribute(el, attribute) → {string}

获取元素对应属性值

Parameters:
Name Type Description
el Element

A DOM element

attribute string

Attribute to get the value of

Returns:
string -

value of the attribute

static getAttributes(tag) → {Object}

获取元素所有属性值,对于Boolean属性则返回true或false

Parameters:
Name Type Description
tag Element

Element from which to get tag attributes.

Returns:
Object -

All attributes of the element.

static getBoundingClientRect(el) → {Object|undefined}

类似原生getBoundingClientRect 方法,使用前考虑兼容性。不支持老浏览器如IE8。
另外,一些浏览器不支持给 ClientRect / DOMRect 对象添加属性。通过浅拷贝标准属性(除了像 x y 这种广泛支持的属性) 来实现。

Parameters:
Name Type Description
el Element

Element whose ClientRect we want to calculate.

Returns:
Object | undefined -

Always returns a plain

static getPointerPosition(el, event) → {Dom~Coordinates}

基于元素的bottom和left坐标,获取鼠标在元素中的位置信息,返回一个包含x和y的坐标对象。

Parameters:
Name Type Description
el Element

Element on which to get the pointer position on

event EventTarget~Event

Event object

Returns:
Dom~Coordinates -

A Coordinates object corresponding to the mouse position.

static hasClass(element, classToCheck) → {boolean}

检查元素是否有css class

Parameters:
Name Type Description
element Element

Element to check

classToCheck string

Class name to check for

Throws:

Throws an error if classToCheck has white space.

Type
Error
Returns:
boolean -
  • True if the element had the class
      - False otherwise.

static insertContent(el, content) → {Element}

格式化内容,先清空再插入到元素
Normalizes and inserts content into an element; this is identical to
appendContent(), except it empties the element first.

Parameters:
Name Type Description
el Element

Element to insert normalized content into.

content String | Element | TextNode | Array | function

See the content argument of dom:normalizeContent

Returns:
Element -

The element with inserted normalized content.

static isEl(value) → {boolean}

用过鸭子类型检查是否为DOM元素

Parameters:
Name Type Description
value Mixed

The thing to check

Returns:
boolean -
  • True if it is a DOM element
      - False otherwise

static isInFrame() → {boolean}

检查当前DOM是否在iframe

Returns:
boolean

static isReal() → {Boolean}

检查当前DOM是否为真实的DOM接口

Returns:
Boolean

static isSingleLeftClick(event) → {boolean}

检查是否为左键单击

Parameters:
Name Type Description
event EventTarget~Event

Event object

Returns:
boolean -
  • True if a left click
      - False if not a left click

static isTextNode(value) → {boolean}

通过鸭子类型来判断是否为文本节点

Parameters:
Name Type Description
value Mixed

Check if this value is a text node.

Returns:
boolean -
  • True if it is a text node
      - False otherwise

static normalizeContent(content) → {Array}

格式化插入DOM的内容
防止用innerHTML造成的xss攻击
内容可以多种类型和组合,如下

Parameters:
Name Type Description
content String | Element | TextNode | Array | function
  • String: Normalized into a text node.
     - Element/TextNode: Passed through.
     - Array: A one-dimensional array of strings, elements, nodes, or functions
       (which return single strings, elements, or nodes).
     - Function: If the sole argument, is expected to produce a string, element,
       node, or array as defined above.
Returns:
Array -

All of the content that was passed in normalized.

static prependTo(child, parent)

在父元素向前插入子元素

Parameters:
Name Type Description
child Element

Element to insert

parent Element

Element to insert child into

static removeAttribute(el, attribute)

移除属性值

Parameters:
Name Type Description
el Element

A DOM element

attribute string

Attribute to remove

static removeClass(element, classToRemove) → {Element}

移除元素对应class

Parameters:
Name Type Description
element Element

Element to remove a class name from.

classToRemove string

Class name to remove

Returns:
Element -

The dom element with class name removed.

static setAttribute(el, attribute, value)

设置属性值

Parameters:
Name Type Description
el Element

A DOM element

attribute string

Attribute to set

value string

Value to set the attribute to

static setAttributes(el, attributesopt)

给元素设置属性

Parameters:
Name Type Attributes Description
el Element

Element to add attributes to.

attributes Object <optional>

Attributes to be applied.

static textContent(el, text) → {Element}

把文本插入元素,并且替换所有已存在的内容

Parameters:
Name Type Description
el Element

The element to add text content into

text string

The text content to add.

Returns:
Element -

The element with added text content.

static toggleClass(element, classToToggle, predicateopt) → {Element}

通过判断class是否存在来给元素添加或移除对应类

Parameters:
Name Type Attributes Description
element Element

The element to toggle a class name on.

classToToggle string

The class that should be toggled

predicate boolean | PredicateCallback <optional>

See the return value for Dom~PredicateCallback

Returns:
Element -

The element with a class that has been toggled.

static unblockTextSelection()

取消禁用文本选择

inner classRegExp(className) → {RegExp}

生成匹配元素className的正则表达式

Parameters:
Name Type Description
className string

The className to generate the RegExp for.

Returns:
RegExp -

The RegExp that will check for a specific className in an elements
className.

inner createQuerier(method) → {function}

通过传入的方法来查询DOM

Parameters:
Name Type Description
method string

The method to create the query with.

Returns:
function -

The query method

inner isNonBlankString(str) → {boolean}

检查字符串是否没有空格

Parameters:
Name Type Description
str string

The string to check

Returns:
boolean -
  • True if the string is non-blank
      - False otherwise

inner throwIfWhitespace(str)

如果字符串有空格则抛出错误。用于让类方法和classList API 保持一致

Parameters:
Name Type Description
str string

The string to check for whitespace.

Throws:

Throws an error if there is whitespace in the string.

Type
Error

Type Definitions

Position

获取DOM元素在界面中的位置信息

Properties:
Name Type Description
left number

Pixels to the left

top number

Pixels on top