【问题标题】:Change class of another element using template reference variable使用模板引用变量更改另一个元素的类
【发布时间】:2017-05-16 12:36:41
【问题描述】:

我有这个组件模板:

<div id="one" (click)=""></div>
<div id="two" #two></div>

我应该在(click)="" 中添加什么来为第二个 div 分配一些类。我知道它可以在组件代码中完成,但是有没有办法直接在模板中完成?

【问题讨论】:

    标签: angular angular2-template


    【解决方案1】:
    (click)="two.className = 'someClass'"
    

    (click)="two.classList.add('someClass')"
    

    另见Change an element's class with JavaScript

    【讨论】:

      【解决方案2】:
      <div id="one" (click)="document.getElementById('#two').classList.add('soemClass')"></div>
      <div id="two" #two></div>
      

      这可能对你有帮助!

      【讨论】:

        猜你喜欢
        • 2015-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-30
        • 1970-01-01
        • 1970-01-01
        • 2013-09-25
        • 2015-07-11
        相关资源
        最近更新 更多