1 Star 0 Fork 10

hutubaba/dom.js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
readonly.js 1.17 KB
一键复制 编辑 原始数据 按行查看 历史
Yaohaixiao 提交于 2023-10-24 11:41 . chore: 打包更新
import isElement from './isElement'
import getAttribute from './getAttribute'
import removeAttribute from './removeAttribute'
import setAttribute from './setAttribute'
import getStyle from './getStyle'
import { READONLY_INPUT_TYPES } from './utils/enum'
/**
* 启用或者取消 DOM 元素的只读模式
* ========================================================================
* @method readonly
* @since 1.2.0
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly
* @param el
* @return {boolean}
*/
const readonly = (el) => {
const TAGS = ['input', 'textarea']
let tagName
let type
if (!isElement(el)) {
return false
}
tagName = el.tagName.toLowerCase()
if (TAGS.indexOf(tagName) === -1) {
return false
}
if (tagName === 'input') {
type = getAttribute(el, 'type').toLowerCase()
if (
READONLY_INPUT_TYPES.indexOf(type) === -1 ||
getStyle(el, 'display') === 'hidden'
) {
return false
}
}
if (getAttribute(el, 'readonly')) {
removeAttribute(el, 'readonly')
} else {
setAttribute(el, 'readonly', true)
}
}
export default readonly
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/hutu88/dom.js.git
git@gitee.com:hutu88/dom.js.git
hutu88
dom.js
dom.js
main

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385