0

现在我这样做:

NodeList* elementsUsingTagG = [third_floor.DOMDocument getElementsByTagName:@"g"];
auds = [elementsUsingTagG.internalArray objectAtIndex:1];
SVGElement *elem = (SVGElement*)[third_floor.DOMDocument getElementById:hitLayer.name];
if ([auds.childNodes.internalArray containsObject:elem])
{
    [self deselectTappedLayer];
    lastTappedLayer = hitLayer;

    if (lastTappedLayer != nil)
    {
        lastTappedLayer.opacity = 1.0f;
        NSLog(@"Clicked on auditory %@, highlight it!", hitLayer.name);
    }
}

我可以获得 SVGElement/CALayer/Node 的一些属性,它标识了这个元素所属的组吗?谢谢

4

1 回答 1

1

我猜是这样的。我还没有测试代码,但我希望它可以帮助你。

迭代版本:

- (Element *)findFirstParentElementFromNode:(Node *)node
                                withTagName:(NSString *)tagName {
    for (node = node.parentNode; node != nil; node = node.parentNode) {
        if (![node isKindOfClass:[Element class]]) {
            continue;
        }

        Element *element = (Element *)node;
        if([element.tagName isEqualToString:tagName]) {
            return element;
        }
    }

    return nil;
}

递归版本:

- (Element *)findFirstParentElementFromNode:(Node *)node
                                withTagName:(NSString *)tagName {
    if (node.parentNode == nil) {
        return nil;
    }

    if ([node.parentNode isKindOfClass:[Element class]]) {
        Element *element = (Element *)node.parentNode;
        if([element.tagName isEqualToString:tagName]) {
            return element;
        }
    }

    return [self findFirstParentElementFrom:node.parentNode
                                withTagName:tagName];
}

并在您的情况下使用其中一个:

Element *groupElement = [self findFirstParentElementFromNode:elem
                                                 withTagName:@"g"];
于 2013-03-17T19:19:56.697 回答