getElementById()通配符 [英] getElementById() wildcard

查看:778
本文介绍了getElementById()通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div,并且我有一些未定义级别的子节点。

I got a div, and there i got some childnodes in undefined levels.

现在我必须将每个元素的ID更改为一个div。如何实现?

Now I have to change the ID of every element into one div. How to realize?

我想,因为他们有上传ID,所以如果父亲是id ='path_test_maindiv'那么下一个下行者将是'path_test_maindiv_child',因此我想,我会通过通配符解决这个问题,例如:

I thought, because they have upgoing IDs, so if the parent is id='path_test_maindiv' then the next downer would be 'path_test_maindiv_child', and therefore I thought, I'd solve that by wildcards, for example:

document.getElementById('path_test_*')

这可能吗?或者还有其他方法吗?

Is this possible? Or are there any other ways?

推荐答案

不在本机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 ;)

这篇关于getElementById()通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆