根据id/类名/元素名称查找元素

/**
* 根据id/类名/元素名称查找元素
* @param selector 选择器(#id 、.className、tagname)
* @return 返回根据选择器条件查找到的元素
*/
function $(selector) {
  if (selector.indexOf("#") === 0) // id
    return document.getElementById(selector.slice(1))
  if (selector.indexOf(".") === 0) // className
    return byClass(selector.slice(1));
  // element
  return document.getElementsByTagName(selector);
}

猜你喜欢

转载自www.cnblogs.com/kdiekdio/p/10223890.html