【问题标题】:Get element parent height inside directive in AnguarJS在AngularJS中的指令内获取元素父高度
【发布时间】:2015-08-06 01:30:46
【问题描述】:

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

这是我现在拥有的,显然它不起作用。

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

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

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

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

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

    或者你可以在纯 JavaScript 中使用 parentNode 属性,这是对父元素的引用:

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

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

    【讨论】:

    • 啊,太好了。非常感谢它现在有效!看来我只需要添加括号..
    【解决方案2】:

    e.parent 是一个函数,所以你必须这样调用它:

    e.parent().height(1200);
    

    另外,如果你没有在页面上加载jquery,你将不得不使用

    .css('height', '1200px')
    

    相反,因为 jqLit​​e 不包括 .height

    【讨论】:

      猜你喜欢
      • 2015-03-09
      • 1970-01-01
      • 2016-12-02
      • 2016-12-07
      • 2017-02-10
      • 2012-11-06
      • 2015-12-09
      • 2015-09-21
      • 2014-03-20
      相关资源
      最近更新 更多