JavaScript - 改变CSS颜色5秒 [英] JavaScript -Change CSS color for 5 seconds

查看:491
本文介绍了JavaScript - 改变CSS颜色5秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

< a href =#faq1onClick =document.getElementById('faq1')。style.color ='#f00'>

因此,在常见问题列表中,跳转到正确的ID,将颜色更改为红色几秒钟作为最终用户的视觉提示(答案在这里)。



如何使上述功能只在一段时间内工作?

解决方案

尝试:

  ){
var orig = obj.style.color;
obj.style.color ='#f00';
setTimeout(function(){
obj.style.color = orig;
},5000);
}

和html:

 < a href =#faq1onClick =highlight(document.getElementById('faq1'));> 

此函数将适用于任何传递给它的对象: - )



这里是一个工作小提琴: http://jsfiddle.net/maniator/dG2ks/


I am trying to make a way of briefly highlighting the text on named links - but only for a few seconds.

<a href="#faq1"onClick="document.getElementById('faq1').style.color='#f00'">

So on a list of FAQs - it jumps to the proper ID, changes the color to red for a few seconds as a visual cue to the end user (the answer is here). but then returning to normal color and the interval is complete.

How do I make the above function to work for only a set period of time?

解决方案

try this:

function highlight(obj){
   var orig = obj.style.color;
   obj.style.color = '#f00';
   setTimeout(function(){
        obj.style.color = orig;
   }, 5000);
}

and in the html:

<a href="#faq1" onClick="highlight(document.getElementById('faq1'));">

this function will work for any object you pass to it :-)

here is a working fiddle: http://jsfiddle.net/maniator/dG2ks/

这篇关于JavaScript - 改变CSS颜色5秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆