使用Javascript改变背景颜色时文本会发生变化 [英] Text change when background colour changes using Javascript

查看:114
本文介绍了使用Javascript改变背景颜色时文本会发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我用于更改网站背景颜色的Javascript,但是当有颜色发生变化时,有没有办法更改页面正文中的文字?

Below is my Javascript for changing the background colour of a website, however is there a way to change text in the body of the page when this change in colour occurs?

function changeColor(element, curNumber){
curNumber++;

if(curNumber > 4){
    curNumber = 1;
}
console.log(curNumber);
element.addClass('color' + curNumber, 500);
// So previous classes get removed.
element.attr('class', 'color' + curNumber);
setTimeout(function(){changeColor(element, curNumber)}, 1000);  
}
changeColor($('#testElement'), 0);


推荐答案

只需将数组中的文本从一些使用jquery的元素 - 但是你想要它)迭代它并在你改变颜色时使用 html()函数更改div中的内容。

Simply have the text in an array, ( get it from some elements using jquery - however you want it) iterate through it and change the content inside the div using html() function when you are changing the color.

对你的代码进行简单的修改就像

Simple modification of your code would be something like

function changeColorAndText(element, curNumber){
 curNumber++;
 if(curNumber > 4){
    curNumber = 1;
 }
 element.addClass('color' + curNumber, 500);
 element.html(arr[curNumber]);
 element.attr('class', 'color' + curNumber);
 setTimeout(function(){changeColorAndText(element, curNumber)}, 1000);  
}
var arr = ['hi','hello ','how ','are ','you '];
changeColorAndText($('#testElement '), 0);

JSFiddle

这篇关于使用Javascript改变背景颜色时文本会发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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