首页 > 其他 > 详细

for循环 事件 if else综合应用

时间:2019-09-20 22:43:57      阅读:117      评论:0      收藏:0      [点我收藏+]
    <table id="tb">
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
        </tr>
        <tr>
            <td>4</td>
            <td>5</td>
            <td>6</td>
        </tr>
    </table>
    <script>
        var td = document.getElementsByTagName(td);
        // console.log(td);
        for (var a in td{ (a约等于for循环的名称,引用时放在[]中,不能再忘记了)
            td[a].onclick = function () {
                // this.style.backgroundColor=‘red‘;(点击颜色不消失)
                if (this.style.backgroundColor{
                    this.style.backgroundColor = ""
                }
                else {
                    this.style.backgroundColor = "red"
                }

            }
        }
<----------------------------------------------------鼠标移上去变色移走颜色还原---------------------------------------------------------------->
(用到两个方法)
        var td=document.getElementsByTagName(td);
        for(var i in td){
            td[i].onmouseover=function(){
                this.style.backgroundColor="red";}
            }
        for(var i in td){
            td[i].onmouseout=function(){
                this.style.backgroundColor="";}
            }

for循环 事件 if else综合应用

原文:https://www.cnblogs.com/xingzhikun12138/p/11559908.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!