[英]How to indicate that that something has been clicked by changing color of something else?
Here I want the color of text +Timer to change once user clicks on stopwatch or timer. 在这里,我希望一旦用户单击秒表或计时器,文本+ Timer的颜色就会改变。 I tried to use some JavaScript but could not implement it. 我尝试使用一些JavaScript,但无法实现。
Here is my code 这是我的代码
$(".icon-clock").shieldButton({
events: {
click: function () {
clearInterval(timer);
startTime = Date.now();
timer = setInterval(updateProgress, 100);
$(".colorchange").color(rgb(#0F0));
//It is not going to work I know
}
}
});
Here is the timer part 这是计时器部分
<li><a href="#" class="dropdown-toggle icon-plus" data-toggle="dropdown">
<span data-placement="bottom" title="Show Timer" class="colorchange">+ Timer </span></a>
<ul class="dropdown-menu timer-wrapper" style=" padding:5px;">
<div class="btn-group">
<button title="" type="button" class="btn btn-default b_fix icon-stopwatch" onclick="start(0);" data-original-title="Stopwatch"></button>
<button title="" type="button" class="btn btn-default b_fix icon-clock" onclick="start(1);" data-original-title="Timer"></button>
</div>
尝试:
$(".colorchange").css('color: #0F0');
You are trying to use a method called color()
in jQuery, but I'm afraid this method doesn't exists. 您正在尝试在jQuery中使用一种称为color()
的方法,但恐怕此方法不存在。 Instead, use the method css()
, which is made to change css properties: 而是使用方法css()
,该方法用于更改css属性:
$(".colorchange").css('color', '#00ff00');
Further, to make your code more reusable, I suggest you to create a class for this purpose and add it to the element with addClass()
. 此外,为使代码更可重用,建议您为此创建一个类,然后使用addClass()
将其添加到元素中。 Unless you have to change to many different colors. 除非您必须更改为许多不同的颜色。
You may use jQuerys css
method even with an object with several style definitions: 您甚至可以使用具有多个样式定义的对象使用jQuery的css
方法:
$('.colorchange').css({
'color' : '#0f0',
'font-weight' : bold
});
It would be an even better solution if you defined several styles for the element that should change the color. 如果您为应该更改颜色的元素定义了几种样式,那将是一个更好的解决方案。 You would only need to change the objects class name. 您只需要更改对象类名称。 Put this into your css: 把它放进你的css:
.colorchange {
color: #666;
font-weight: normal;
}
.colorchange.highlight {
color: #0f0;
font-weight: bold;
}
And this in your javascript: 而这在您的javascript中:
$('.colorchange').addClass('highlight');
This solution would be more future proof as you could manage all the styles in your css and all the logic in your javascript. 由于您可以管理CSS中的所有样式以及javascript中的所有逻辑,因此该解决方案将为将来提供更多证明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.