導航:首頁 > 編程語言 > 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交替改變顏色相關的資料

熱點內容
設置hdd密碼 瀏覽:624
iphone4s升級ios9失敗怎麼辦 瀏覽:6
ro裝備有洞升級後 瀏覽:958
不綁定銀行卡微信轉賬 瀏覽:827
編程和網站有什麼區別 瀏覽:443
java兩個集合取交集 瀏覽:442
js替換當前頁面字元串 瀏覽:50
ps6路徑選擇工具 瀏覽:955
視頻文件損壞返回不了 瀏覽:656
如何判斷網路被降速了 瀏覽:676
ps做成圖片後怎麼弄成文件 瀏覽:689
左側導航欄css代碼 瀏覽:952
釘釘傳送文件最大多少兆 瀏覽:126
app下載哪裡最全 瀏覽:599
word如何畫大箭頭 瀏覽:245
word批量轉pdf工具21注冊機 瀏覽:546
列印文檔文件3000字需要多少錢 瀏覽:239
泊車助手app 瀏覽:147
pscs6完全自學教程 瀏覽:461
文件夾去不掉只讀屬性 瀏覽:203

友情鏈接