希望我能解释清楚。我愿意
var foo = document.getElementsByTagName('bar');
var len = foo.length;
for(var i=0;i<len;i++){
//Do stuff
}
在其中,我还希望能够获得深埋在 foo[i] 中的元素(特别是类)。我的想法是这样的
var whatIWant = document.getElementsByClassName("name").foo[i];
但这似乎不是我需要做的。我只是不确定语法还是我需要做一些完全不同的事情?
最佳答案
你快搞定了:
foo = document.getElementsByTagName('bar');
// foo is now a nodelist of all nodes named 'bar'.
for (i = 0; i < foo.length; i++) {
bar = foo[i].getElementsByClassName('baz');
// bar is now a nodelist of all elements with class 'baz' in the dom tree under foo[i].
}
如果你使用 jquery,它会很简单:
$('bar .baz').each(function() {
$(this). etc...;
});
关于javascript - 如何抓取埋在你抓取的元素中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9354678/