導航:首頁 > 編程語言 > js交替改變顏色

js交替改變顏色

發布時間:2024-01-31 13:01:16

js怎麼實現點擊循環切換顏色

你可以逗磨先將要出現的顏色,存儲到一個數組里,點擊時乎鏈切換數組里歲指孫的元素就可以了。

下面是小例子:

<body>
<divstyle="width:100px;height:100px;border:1pxsolid#ccc;"></div>
</body>
<script>
varsColor=['red','blue','yellow','gray'];
varoDiv=document.getElementsByTagName('div')[0];
variNum=0;
oDiv.onclick=function(){
this.style.background=sColor[iNum%sColor.length];
iNum++;
};
</script>

Ⅱ 怎麼用js點擊按鈕改變網頁主題,就是把顏色換一下,再次點擊在換回來,就這樣點擊來回切換

 <!DOCTYPEhtml>
<htmllang="en">

<head>
<metacharset="UTF-8">
<metaname="viewport"content="width=device-width,initial-scale=1.0">
<title>Document</title>
<style>
body{
background-color:green;
}
</style>
</head>

<body>
<button>btn</button>
</body>
<script>
letisGreen=false;
document.querySelector("button").onclick=()=>{
document.body.style.backgroundColor=isGreen?"green":"red"
isGreen=!isGreen;
}
</script>

</html>

 請採納

Ⅲ 需求一個js 或者 jq 通過點擊一個按鈕,來改變2個div的背景顏色

<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
<title>jquerycookie</title>
<scriptsrc="http://libs..com/jquery/1.8.0/jquery.min.js"></script>
<title>顏色互換</title>
</head>
<body>
<style>
.div1{width:100px;height:100px;background:#00F;}
.div2{width:100px;height:100px;background:#f00;}
.div3{width:100px;height:100px;background:#3F6;;}
</style>
<divclass="div1"><inputname=""type="checkbox"value="a">AAAAAA</div>
<br>
<divclass="div2"><inputname=""type="checkbox"value="b">BBBBBB</div>
<br>
<divclass="div3"><inputname=""type="checkbox"value="c">cccccc</div>
<br>
<inputname=""type="button"value="點擊">
<script>
$(function(){
$("input:button").click(function(){
varlen=$(":checked").length;
if(len<=1){
alert("請選擇兩種顏色");
}elseif(len>2){
alert("不能超過兩種顏色");
}else{
varbackground1=$(":checked").eq(0).parent().css("background");
varbackground2=$(":checked").eq(1).parent().css("background");
$(":checked").eq(0).parent().css("background",background2);
$(":checked").eq(1).parent().css("background",background1);
}
});
});
</script>
</body>
</html>

Ⅳ js點擊切換背景顏色

<script>
window.onload=document.onclick=()=>{
let c=()=>parseInt(Math.random()*256);
document.body.style.backgroundColor="rgb("+c()+","+c()+","+c()+")";
}
</script>

Ⅳ 用JS實現變換背景和字體顏色,分別保存紅、藍、綠三種顏色,每隔一秒鍾變換一個顏色,顏色從數組中獲取

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery-1.5.1.js"></script>
<script>
//setTimeout
var colors = new Array("red","green","blue");

function changeColor(){
//var colorIndex = Math.round(Math.random()*3);
var color_div=colors[Math.round(Math.random()*3)];
var color_font=colors[Math.round(Math.random()*3)];
document.getElementById("div_c").style.background=color_div;
document.getElementById("font_c").style.color=color_font;
//document.wirte(colorIndex);
setTimeout("changeColor()",1000);
}
</script>
</head>
<body onload="changeColor()">
<div id="div_c" align="center" style="height:1000px;width:100%">
<br/>
<br/>
<br/>
<font id="font_c" size="20px" >自動變換顏色</font>
</div>
</body>
</html>

Ⅵ 如何在JSP中實現表格顏色交替出現

哈哈,幫你解決了問題,而且還要感謝你,我學會了CSS中的expression

解決方法如下:

<style>

td,th{

background:expression((this.parentNode.rowIndex)%2==0?"#3DB7CC":"#B3F4FF");

}

</style>

在<head>...</head>加上這段代碼,就可以適用任何網頁,其中#......是顏色的代碼,可以隨意搭配,順便給你截張圖

祝咱倆都學習進步!

閱讀全文

與js交替改變顏色相關的資料

熱點內容
wifisd的密碼 瀏覽:694
把excel表格插入word 瀏覽:117
經發e家app忘記密碼怎麼辦 瀏覽:921
goagent教程 瀏覽:344
桌面文件全選按鍵 瀏覽:105
PM2019支持哪些格式文件 瀏覽:272
全拼app現在叫什麼名字 瀏覽:476
win10現在成熟了嗎 瀏覽:727
win10mtp段落無效 瀏覽:862
cbf收銀機用什麼app 瀏覽:336
javascript變數頁面 瀏覽:766
網路相冊淘寶 瀏覽:60
編程拉絲機怎麼樣 瀏覽:897
msdn不是最新版本 瀏覽:749
參加編程比賽學什麼編程好 瀏覽:349
把c盤的桌面文件移走了怎麼辦 瀏覽:949
2025第三代軍用北斗升級版 瀏覽:925
魅藍手勢密碼怎麼弄 瀏覽:509
蘋果store怎麼充值 瀏覽:224
note4應用程序許可設置在哪裡 瀏覽:489

友情鏈接