dom.js
Members
-
static, constant $
-
用匹配选择器获取单个DOM元素,依赖于其他DOM的
context,默认为document -
static, constant $$
-
获取所有匹配选择器的元素
Methods
-
static addClass(element, classToAdd) → {Element}
-
给元素加对应class
Parameters:
Name Type Description elementElement Element to add class name to.
classToAddstring Class name to add.
Returns:
Element -The dom element with the added class name.
-
static appendContent(el, content) → {Element}
-
格式化文本并添加到元素
Parameters:
Name Type Description elElement Element to append normalized content to.
contentString | Element | TextNode | Array | function See the
contentargument of dom:normalizeContentReturns:
Element -The element with appended normalized content.
-
static blockTextSelection()
-
拖动控件时阻止选择文本
-
static createEl(tagNameopt, propertiesopt, attributesopt, contentopt) → {Element}
-
用所给属性创建元素
Parameters:
Name Type Attributes Default Description tagNamestring <optional>
'div' Name of tag to be created.
propertiesObject <optional>
{} Element properties to be applied.
attributesObject <optional>
{} Element attributes to be applied.
contentString | 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 elElement 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 ResigParameters:
Name Type Description elElement Element from which to get offset
-
static getAttribute(el, attribute) → {string}
-
获取元素对应属性值
Parameters:
Name Type Description elElement A DOM element
attributestring Attribute to get the value of
Returns:
string -value of the attribute
-
static getAttributes(tag) → {Object}
-
获取元素所有属性值,对于Boolean属性则返回true或false
Parameters:
Name Type Description tagElement Element from which to get tag attributes.
Returns:
Object -All attributes of the element.
-
static getBoundingClientRect(el) → {Object|undefined}
-
类似原生
getBoundingClientRect方法,使用前考虑兼容性。不支持老浏览器如IE8。
另外,一些浏览器不支持给ClientRect/DOMRect对象添加属性。通过浅拷贝标准属性(除了像xy这种广泛支持的属性) 来实现。Parameters:
Name Type Description elElement Element whose
ClientRectwe want to calculate.Returns:
Object | undefined -Always returns a plain
-
static getPointerPosition(el, event) → {Dom~Coordinates}
-
基于元素的bottom和left坐标,获取鼠标在元素中的位置信息,返回一个包含x和y的坐标对象。
Parameters:
Name Type Description elElement Element on which to get the pointer position on
eventEventTarget~Event Event object
-
static hasClass(element, classToCheck) → {boolean}
-
检查元素是否有css class
Parameters:
Name Type Description elementElement Element to check
classToCheckstring Class name to check for
Throws:
-
Throws an error if
classToCheckhas 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 toappendContent(), except it empties the element first.Parameters:
Name Type Description elElement Element to insert normalized content into.
contentString | Element | TextNode | Array | function See the
contentargument of dom:normalizeContentReturns:
Element -The element with inserted normalized content.
-
static isEl(value) → {boolean}
-
用过鸭子类型检查是否为DOM元素
Parameters:
Name Type Description valueMixed The thing to check
Returns:
boolean -- True if it is a DOM element
- False otherwise
- True if it is a DOM element
-
static isInFrame() → {boolean}
-
检查当前DOM是否在iframe
Returns:
boolean -
static isReal() → {Boolean}
-
检查当前DOM是否为真实的DOM接口
Returns:
Boolean -
static isSingleLeftClick(event) → {boolean}
-
检查是否为左键单击
Parameters:
Name Type Description eventEventTarget~Event Event object
Returns:
boolean -- True if a left click
- False if not a left click
- True if a left click
-
static isTextNode(value) → {boolean}
-
通过鸭子类型来判断是否为文本节点
Parameters:
Name Type Description valueMixed Check if this value is a text node.
Returns:
boolean -- True if it is a text node
- False otherwise
- True if it is a text node
-
static normalizeContent(content) → {Array}
-
格式化插入DOM的内容
防止用innerHTML造成的xss攻击
内容可以多种类型和组合,如下Parameters:
Name Type Description contentString | 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.
- String: Normalized into a text node.
-
static prependTo(child, parent)
-
在父元素向前插入子元素
Parameters:
Name Type Description childElement Element to insert
parentElement Element to insert child into
-
static removeAttribute(el, attribute)
-
移除属性值
Parameters:
Name Type Description elElement A DOM element
attributestring Attribute to remove
-
static removeClass(element, classToRemove) → {Element}
-
移除元素对应class
Parameters:
Name Type Description elementElement Element to remove a class name from.
classToRemovestring Class name to remove
Returns:
Element -The dom element with class name removed.
-
static setAttribute(el, attribute, value)
-
设置属性值
Parameters:
Name Type Description elElement A DOM element
attributestring Attribute to set
valuestring Value to set the attribute to
-
static setAttributes(el, attributesopt)
-
给元素设置属性
Parameters:
Name Type Attributes Description elElement Element to add attributes to.
attributesObject <optional>
Attributes to be applied.
-
static textContent(el, text) → {Element}
-
把文本插入元素,并且替换所有已存在的内容
Parameters:
Name Type Description elElement The element to add text content into
textstring 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 elementElement The element to toggle a class name on.
classToTogglestring The class that should be toggled
predicateboolean | 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 classNamestring The className to generate the RegExp for.
Returns:
RegExp -The RegExp that will check for a specific
classNamein an elements
className. -
inner createQuerier(method) → {function}
-
通过传入的方法来查询DOM
Parameters:
Name Type Description methodstring The method to create the query with.
Returns:
function -The query method
-
inner isNonBlankString(str) → {boolean}
-
检查字符串是否没有空格
Parameters:
Name Type Description strstring The string to check
Returns:
boolean -- True if the string is non-blank
- False otherwise
- True if the string is non-blank
-
inner throwIfWhitespace(str)
-
如果字符串有空格则抛出错误。用于让类方法和classList API 保持一致
Parameters:
Name Type Description strstring 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 leftnumber Pixels to the left
topnumber Pixels on top