js实现开和关两个按钮的切换,并且背景颜色也变

如题所述

第1个回答  2015-08-25

<body>

<button  id="btn">开</button>


<script>


        var btnE = document.getElementById('btn'),

                text;

            btnE.addEventListener('click',function(){

                text = btnE.innerText;

                btnE.innerText = text === "开" ? "关":"开";

                text === "开" ? document.body.style.backgroundColor = "red":document.body.style.backgroundColor = "white";



            });



</script>

</body>


本回答被网友采纳
相似回答