且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

getElementById()通配符

更新时间:2023-11-27 13:42:22

不在本机JavaScript中。你有各种选择:

Not in native JavaScript. You have various options:

1)放一个类并使用getElementsByClassName但是它不适用于每个浏览器

1) Put a class and use getElementsByClassName but it doesn't work in every browser.

2)制作自己的功能。类似于:

2) Make your own function. Something like:

function getElementsStartsWithId( id ) {
  var children = document.body.getElementsByTagName('*');
  var elements = [], child;
  for (var i = 0, length = children.length; i < length; i++) {
    child = children[i];
    if (child.id.substr(0, id.length) == id)
      elements.push(child);
  }
  return elements;
}

3)使用库或CSS选择器。像jQuery;)

3) Use a library or a CSS selector. Like jQuery ;)