-1

我可以像这样访问我的父元素:

var parentElem = angular.element('#parent');

然后在这个下面的点击事件中,我想检查点击是否是由上面父母的任何孩子产生的。

$('html').click(function (e) {

  // check e.target is child of 'parentElem'           

});
4

1 回答 1

1

您应该能够使用普通的 JavaScript 来实现这一点:

// Get a reference to a raw DOM node
var parentElem = angular.element("#parent")[0];

$("html").click(function(e) {
    // Get a reference to the event target
    var element = e.target;

    // "Climb" up the document tree until you've found the parent,
    // or you can "climb" up no further.
    while (parentElem !== element && element.parentNode) {
        element = element.parentNode;
    }

    if (element === parentElem) {
        // event target is inside parentElem
    }
    else {
        // event target is not a child of parentElem
    }
});
于 2015-07-09T21:07:14.503 回答