Skip to content

@utilslib/web/getAttribute


安全获取属性值 ​


getAttribute ​

函数签名 ​

typescript
function getAttribute(element: Element, attributeName: string): string | null

描述 ​

安全获取属性值

参数 ​

参数名类型可选默认值描述
elementElement否--
attributeNamestring否--

返回值 ​

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 :::