简体   繁体   English

如何通过更改其他颜色来指示已单击某些内容?

[英]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.

相关问题 将网址更改为其他内容? - Changing URL to something else? 在 JS 中检查一个按钮是否被点击,然后在 PHP 中做一些事情 - Check a button has been clicked in JS and then do something in PHP 如何确定第一次加载时是否在jQuery中单击了某些内容? - How do I determine if something has been clicked in jQuery on the first load? 如果启用了另一个按钮,如何编写知道单击了一个按钮并执行某些操作的函数? - How to write a function that knows a button has been clicked that executes something if another button is enabled? 如何检查页面中的内容是否已更改? - How to check that something has been changed in the page? 如何在调用函数或执行其他操作之前更改背景颜色 - How to change background color before calling a function or doing something else 如何实现也具有其他原型的功能 - How to implement a function that also has a prototype of something else jquery“if”something和“else”的东西 - jquery “if” something and “else” something 我的一组函数执行完后该怎么办? - How to do something after my set of functions has been executed? 在 create JS 中 drawPolygon 的替代方法是什么? 此功能是否已更改为其他功能? - What is the alternate for drawPolygon in create JS ? Has this function been changed to something else?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM