【问题标题】:blur header element in HTML through angularJS directives通过 angularJS 指令模糊 HTML 中的标题元素
【发布时间】:2015-10-22 02:32:35
【问题描述】:

我有一个按钮。单击该按钮时,我想模糊我的 HTML header 元素中的所有内容。

我想通过指令来完成这个,看起来是这样的

.directive('setUpBlur', function(){
        return {
            restrict: 'A',
            scope: {
                classElement: '@'
            },
            link: function(scope, element, attrs) {
                element.find(scope.classElement).css("-webkit-filter", "blur(10px)");
            }
       };

    });

标题中的按钮如下所示:

<button set-up-blur class-element="header">Generate</button>

但是什么也没发生:

如果我编写以下 CSS,它可以工作:

header {
    -webkit-filter: blur(10px);
}

我错过了什么?

【问题讨论】:

  • 你想通过css类找到吗? element.find() 是否返回任何内容?

标签: javascript html angularjs css angularjs-directive


【解决方案1】:

试试这个:

.directive('setUpBlur', function($rootElement){
        return {
            restrict: 'A',
            scope: {
                classElement: '@'
            },
            link: function(scope, element, attrs) {
                $rootElement.find(scope.classElement).css("-webkit-filter", "blur(10px)");
            }
       };

    });

【讨论】:

  • 那行得通 - 谢谢 :) 但是为什么$rootElement。为什么不element
  • element 指的是按钮元素,'find' 查找子元素。我假设您的 scope.classElement 不是 Button 的子元素。
  • 另外,如果我只想在按下按钮后执行此操作?我试图在控制器中创建一个函数并在 html 中单击 ng-click,但是与 dom 相关的东西应该在指令中?问题是该指令被立即调用
猜你喜欢
  • 2016-12-11
  • 1970-01-01
  • 1970-01-01
  • 2016-09-16
  • 1970-01-01
  • 2020-03-31
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多