在角度2中,我试图更改第三次单击的复选框
因此,在第三次单击的勾号之后,该小方框应为红色。
你能告诉我如何实现它吗?
在下面提供我的代码。
https://stackblitz.com/edit/angular-3s6hsh?file=app%2Fapp.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = 'Angular 5';
//var count = 0;
// public items: Array<string>;
public count = 0;
constructor() {
// this.items = ["item1", "item2", "item3"]
}
getStyle(event) {
// var target = event.target || event.srcElement || event.currentTarget;
// console.log("target--->" , target);
// var idAttr = target.attributes.id;
// console.log("idAttr--->" , idAttr);
// var value = idAttr.nodeValue;
// console.log("#"+value);
const style = `color: red`;
console.log("style--->" + style);
return style;
}
public open(event) {
let count = 0;
let countIncreased = count + 1;
if (count % 3 == 0) {
console.log("getStyle started--->");
var target = event.target || event.srcElement || event.currentTarget;
console.log("target--->", target);
var idAttr = target.attributes.id;
console.log("idAttr--->", idAttr);
var value = idAttr.nodeValue;
console.log("#" + value);
value.css(`color: red`);
//value.addCs
this.getStyle(event);
console.log("getStyle ended--->");
}
alert('Open--->' + countIncreased);
}
}
最佳答案
我会这样做(它使用jQuery):
// declare var to count clicks
var counter = 0;
// attach event to button
$('#buttonId').on('click', function () {
counter = counter + 1;
if (counter > 2) {
// apply new style
$('#buttonId').css('background-color', 'red');
}
});
关于javascript - 在第三次单击时为复选框添加颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49418989/