3 Star 54 Fork 14

Yaohaixiao / dom.js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
getAttribute.js 982 Bytes
一键复制 编辑 原始数据 按行查看 历史
import isElement from './isElement'
import isString from './utils/types/isString'
import { FROM_ELEMENT_TAGS } from './utils/enum'
/**
* 获取 DOM 元素的某个 HTML 属性的值
* ========================================================================
* @method getAttribute
* @param {HTMLElement} el
* @param {String} attr
* @return {String|Boolean}
*/
const getAttribute = (el, attr) => {
let tagName
if (!isElement(el) || !isString(attr)) {
return false
}
tagName = el.tagName.toLowerCase()
switch (attr) {
case 'className':
return el.getAttribute('class')
case 'htmlFor':
return el.getAttribute('for')
case 'style':
return el.style.cssText
case 'value':
if (FROM_ELEMENT_TAGS.indexOf(tagName) > -1) {
return el.value
} else {
return el.getAttribute(attr)
}
default:
return el.getAttribute(attr)
}
}
export default getAttribute
JavaScript
1
https://gitee.com/yaohaixiao/dom.js.git
git@gitee.com:yaohaixiao/dom.js.git
yaohaixiao
dom.js
dom.js
main

搜索帮助