for循环 事件 if else综合应用

你说的曾经没有我的故事 提交于 2019-11-30 04:29:24
    <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="";}
            }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!