安全获取属性值
getAttribute
函数签名
typescript
function getAttribute(element: Element, attributeName: string): string | null描述
安全获取属性值
参数
| 参数名 | 类型 | 可选 | 默认值 | 描述 |
|---|---|---|---|---|
element | Element | 否 | - | - |
attributeName | string | 否 | - | - |
返回值
string \| null
点击查看源码
js
/**
* 安全获取属性值
* @param element - 目标元素
* @param attributeName - 属性名
* @returns 属性值,如果不存在返回 null
*/
export function getAttribute(element, attributeName) {
return element ? element.getAttribute(attributeName) : null;
}ts
/**
* 安全获取属性值
* @param element - 目标元素
* @param attributeName - 属性名
* @returns 属性值,如果不存在返回 null
*/
export function getAttribute(
element: Element,
attributeName: string,
): string | null {
return element ? element.getAttribute(attributeName) : null;
}如有错误,请提交issue :::