简体   繁体   English

DOM帮助,父母的父母,孩子的孩子

[英]DOM help, parents of parents, children of children

I am using Drupal's Webform module which writes all the ids and classes automatically, so I can't add an Id to the place I want to add it. 我正在使用Drupal的Webform模块,该模块自动写入所有ID和类,因此无法将Id添加到要添加的位置。 And Drupal doesn't put one in for me. Drupal不会帮我。 Grrr. rr

I have no problem when I use my code with just a simple getElementById('myid'), but the element I need is a blank, no id, no class "a" that's inside a "legend" (which has a class but no id) that's inside a fieldset with an id. 当我仅将代码与简单的getElementById('myid')一起使用时,我没有问题,但是我需要的元素是空白,无id,在“传奇”(没有类但没有类)中的类“ a” id)位于具有ID的字段集中。

I tried this code and several variations but it didn't work: 我尝试了这段代码和几种变体,但是没有用:

document.getElementById('webform-created-id-here').getElementsByTagName('legend').getElementsByTagName('a');

I feel like I'm not understanding how to properly access that part of the DOM. 我觉得我不了解如何正确访问DOM的那部分。 Can anyone offer help or suggestions? 谁能提供帮助或建议?

Thank you! 谢谢!

getElementsByTagName return a nodelist with the elements found, so you must select the index of the element getElementsByTagName返回具有找到的元素的节点列表,因此必须选择该元素的索引

Example for the first element 第一个元素的示例

var test = document.getElementById('webform-created-id-here').getElementsByTagName('legend')[0].getElementsByTagName('a')[0];

DEMO VIEW 演示视图

Recurse through the DOM yourself. 自己遍历DOM。 References to the children of any element are stored in the childNodes attribute which exist for every node. 对任何元素的子代的引用都存储在每个节点都存在的childNodes属性中。

function recurseDOM (el,test) {
    if (test(el)) {
        return el;
    }
    else {
        var children = el.childNodes;
        var result;
        for (var i=0; i<children.length; i+) {
            result = recurseDOM(children[i],test);
            if (result) {
                return result;
            }
        }
    }
    return null;
}

This is only one possible implementation that I wrote in the 2 minutes it took me to type out this answer. 这只是我在2分钟内输入答案所用的一种可能的实现。 So it can probably use some improvements. 因此,它可能可以使用一些改进。 But you get the idea. 但是你明白了。

Use it like this: 像这样使用它:

recurseDOM(document.body,function(el){
    if (el == something_something) { return 1}
    return 0
});

You can write a similar function to test parents: 您可以编写类似的功能来测试父母:

function testParents (el, test) {
    var parent = el.parentNode;

    if (test(parent)) {
        return 1;
    }
    else {
        if (parent != document.body) {
            return testParents(parent,test);
        }
    }
    return 0;
}

So you can write something like: 因此,您可以编写如下内容:

recurseDOM(document.body,function(el){
    if (el.tagName == 'a' && testParents(el,function(p){
        if (p.tagName == 'legend' && testParents(p,function(pp){
            if (pp.id == 'webform-created-id-here') {
                return 1;
            }
            return 0;
        })) {
            return 1;
        }
        return 0;
    })) {
        return 1;
    }
    return 0;
});

Alternatively, you can start the recursion form the getElementById instead of document.body . 或者,您可以从getElementById而不是document.body开始递归。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM