问题描述
是否有一种方法可以访问以公共字符串开头的所有对象实例.
Is there a way to access all the object instances starting with a common string.
示例:我有名为button64,button223,button856471,button229846等的实例.我无法控制这些实例的命名方式.我想将所有这些都推送到一个数组中.
Example: I have instances named button64, button223, button856471, button229846, etc. I have no control over how these instances are named. I want to push all these in an array.
我不是在创建这些对象,我只需要编写一个JavaScript文件,该JavaScript文件位于此HTML页面中,并收集所有以字符串"button"开头的对象实例.
I am not creating these objects, I just have to write a javascript which sits in this HTML page and collects all the object instances starting with the string 'button'.
目的是从其中伸出一个想要的物体并改变视觉元素.
The purpose is to reach out to a desired object out of these and change the visual element.
此外,该解决方案必须与IE8兼容.关于如何迭代的任何想法?
Also, the solution has to be compatible with IE8. Any ideas about how should I iterate?
推荐答案
如果可以使用jQuery,则可以编写:
If you can use jQuery, you could write:
var buttonsArray = [];
$("div").each(function() {
var id = $(this).attr("id");
if (id && id.indexOf("button") == 0) {
buttonsArray.push($(this));
}
});
这篇关于遍历名称具有公共字符串的所有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!