小编典典

在AnguarJS中获取指令中元素父级的高度

angularjs

如何从指令内部获取和设置元素的父级高度?

这就是我现在所拥有的,显然没有用。

var vAlign = angular.module("vAlign", [])
.directive('vAlign', function() {
  return {
        restrict : "AC",
        link: function(scope, e){

            e.parent.height(1200);
            console.log(e.parent.height);
        }
    };
});

阅读 412

收藏
2020-07-04

共1个答案

小编典典

您可以使用jqLit​​e / jQuery的parentheight方法:

link: function(scope, e) {
    e.parent().height(1200);
    console.log(e.parent().height());
}

或者,您也可以使用带有parentNode属性的纯JavaScript来实现,该属性是对父元素的引用:

link: function(scope, e) {
    e[0].parentNode.style.height = 1200 + 'px';
}

还要注意,由于这里e是一个jqLit​​e /
jQuery实例,它是一个元素的类似数组的集合,因此您需要使用它[0]来访问原始HTMLElement。

2020-07-04