【问题标题】:change background color of div using color picker使用颜色选择器更改 div 的背景颜色
【发布时间】:2018-02-13 09:28:03
【问题描述】:

我已经创建了一个div,当我点击它时,它的颜色从green变成了red。我想添加一个按钮,单击它会出现一个颜色选择器,我选择的颜色将成为它的背景。

下面是我当前的代码

home.html

        <div class="stileone">
            Div Content
        </div>

//I want code on this button click to select color from color picker and selected color shall become the div's background color
    <button ion-button (click)="select_color()">Select Color</button>

home.ts

ngOnInit(){
$(".stileone").on("click", function() {
        $(this).css("background", "red");
    }); 
}

home.scss

.stileone {
    background: green
}

请注意,我在上面的代码中使用了 js,我很好,我可以使用 Ionic 或 JS,两者都适合我。对于 HTML 操作,我使用的是 DOM sanitizer,所以我可以使用 JS 或 jQuery。

【问题讨论】:

标签: javascript angular ionic-framework ionic2 ionic3


【解决方案1】:

我想你只需要:

组件端:

color = 'transparent';

select_color(){
    this.color = this.getRandomColor();
}

模板端:

<div class="stileone"  [ngStyle]="{'background-color': color}">
    Div Content
</div>

WORKING DEMO

【讨论】:

  • 当我点击按钮,调用文本选择器,我需要从中选择,请更新代码来实现这一点
  • 您的代码有效,但我需要实现的方式不起作用,我可以在聊天中分享团队查看器吗?我正在从本地存储中挑选代码,并且在 html 上显示如下 - &lt;div [innerHtml]="code | noSanitize"&gt;&lt;/div&gt;
  • 好吧,如果你可以从html页面上的ts调用html然后让它工作?您将需要一个 domsanitize 管道..因为我正在从本地存储调用 html
  • 我在点击时使用了这个代码` $(".stileone").on("click", function() { $(this).css("background", "red"); }); ` 只需要把它改成颜色选择器
猜你喜欢
  • 2021-07-11
  • 2018-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
相关资源
最近更新 更多