js 点击后变换颜色

js 点击后变换颜色,第1张

windowonload = function() {
    var oDiv = documentgetElementById("tab");
    var oLi = oDivgetElementsByTagName("div")[0]getElementsByTagName("li");
    var aCon = oDivgetElementsByTagName("div")[1]getElementsByTagName("div");
    var timer = null;
    for (var i = 0; i < oLilength; i++) {
        oLi[i]index = i;
        oLi[i]onclick = function() {
            show(thisindex);
        }
    }
    function show(a) {
        index = a;
        var alpha = 0;
        for (var j = 0; j < oLilength; j++) {
            oLi[j]className = "";
            // 加入这句即可
            oLi[index]stylebackgroundColor="transparent";
            aCon[j]className = "";
            aCon[j]styleopacity = 0;
            aCon[j]stylefilter = "alpha(opacity=0)";
        }
        oLi[index]className = "cur";
        // 加入这句即可
        oLi[index]stylebackgroundColor="blue";
        clearInterval(timer);
        timer = setInterval(function() {
            alpha += 2;
            alpha > 100 && (alpha = 100);
            aCon[index]styleopacity = alpha / 100;
            aCon[index]stylefilter = "alpha(opacity=" + alpha + ")";
            alpha == 100 && clearInterval(timer);
        }, 5);
    }
}

<input type="button" id="subBtn" onclick="thisstylebackgroundColor='red';showSubscribe()"
class="but1" value="订阅号管理">
<input type="button" id="msgBtn" onclick="thisstylebackgroundColor='red';showMsgCollection()"
class="but1" value="图文集管理">

<script language="javascript" type="text/javascript">
function chgbg(id){
var obj=windowdocumentgetElementById(id);
var bg=objstylebackgroundColor;
if(bg=='') objstylebackgroundColor='#f00'
else objstylebackgroundColor=''
}
</script>
<table width="100%" border="1" cellspacing="0" cellpadding="4">
<tr>
<td id="td1"> </td>
<td id="td2"> </td>
<td id="td3"> </td>
</tr>
<tr>
<td id="td4"> </td>
<td id="td5"> </td>
<td id="td6"> </td>
</tr>
</table>
<input type="button" value="点击改变指定单元格背景" onClick="chgbg('td1');" />

1、新建一个html文件,命名为testhtml。

2、在testhtml文件内,创建一个div模块,设置其class属性为bg,id属性为mydiv,用于下面设置css样式和获取div对象。

3、在testhtml文件内,使用css设置div的样式,设置其宽度为500px,高度为272px,背景为2jpg。

4、在testhtml文件内,在div的下面创建一个button按钮,按钮名称为“更换背景颜色”。

5、给button按钮绑定onclick点击事件,当按钮被点击时,执行myfun()函数。

6、在testhtml文件内,在js标签内,创建myfun()函数,在函数内,使用getElementById()方法通过id获得div对象,设置对象中的backgroundImage背景属性为另一张,从而实现改变div的背景。就完成了,运行就可以了。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13338939.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-17
下一篇 2023-07-17

发表评论

登录后才能评论

评论列表(0条)

保存