如何抓取埋在你抓取的元素中的元素?
希望我能解释得足够好。我
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];
,但这似乎不是我需要做的。我只是不确定语法还是我需要做一些完全不同的事情?
Hopefully I can explain it well enough. I do
var foo = document.getElementsByTagName('bar');
var len = foo.length;
for(var i=0;i<len;i++){
//Do stuff
}
Inside that for I also want to be able to get an element (specifically a class) that is buried deep within foo[i]. My thought is something like
var whatIWant = document.getElementsByClassName("name").foo[i];
but that doesn't seem to be what I need to do. Am I just not sure on the syntax or do I need to do something completely different?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你几乎已经明白了:
如果你使用 jquery,它会很简单:
You've almost got it:
If you were using jquery, it'd be as simple as:
据我所知 getElementsByClassName 是 HTML5 (根据此: https://developer. mozilla.org/en/DOM/document.getElementsByClassName),因此使用它意味着并非所有浏览器都支持它。
当您从 dom 中获取 Element 时,就像使用 document.getElementsByTagName 一样,您会得到相同的结果方法 getElementsByTagName ,所以你可以这样做:
foo[i].getElementsByTagName("...")
对于所需元素的集合,您可以迭代该数组并搜索匹配的类。
另外,我建议使用 javascript 库,如 jQuery 或 MooTools,它会让你的生活更轻松。
As far as I'm aware getElementsByClassName is HTML5 (according to this: https://developer.mozilla.org/en/DOM/document.getElementsByClassName) and so using it means that not all browser will support it.
When you grab an Element from the dom, like with document.getElementsByTagName, you get the same method getElementsByTagName and so you can do:
foo[i].getElementsByTagName("...")
For the collection of desired elements, then you can iterate over that array and search for the matching classes.
Also, I recommend using javascript libraries such as jQuery or MooTools, it will make your life easier.