javaScript--05 DOM基础 12.2

来源:互联网 发布:可以证件照软件 编辑:程序博客网 时间:2024/04/30 14:56

.disabled="true”.display="none”.removeAttribute("disabled”).innerHTML.style.backgroundColoronfocusonblur


<!DOCTYPE html>

<html lang="zh" id="htmlId">

    <head id="headId">

        <title>BOM基础</title>

        <meta charset="utf-8">

        <meta name="keywords" content=",,">

        <meta name="description" content="">

        <style>

        </style>

    </head>


    <script type="text/javascript">

        function initHideAndShowButton() {

            alert("Onload initHideAndShowButton");

            // 拿到隐藏图片按钮。

            var theHideInput = document.getElementById("hideImgButton");

            // 拿到显示图片按钮。

            var theShowInput = document.getElementById("showImgButton");

            // 初始状态图片正常显示,因此设置显示图片按钮为不可用。

            theShowInput.disabled = "true";

            

            // 拿到图片。

            var theImg = document.getElementById("imgId");

            // 保存初始图片display属性。

            //var defaultDisplayType = theImg.style.display;

            var imgSrc = theImg.src;

            

            // 设置隐藏图片按钮的点击事件对应的方法。

            theHideInput.onclick = function(){

                // 首先隐藏图片。

                //theImg.style.display = "none";

                theImg.src = "";

                // 随后把隐藏图片按钮设为不可用。

                this.disabled = "true";

                // 设置显示图片按钮为可用状态。

                // 此写达不到预计的效果。

                //theShowInput.disabled = "false";

                // 通过删除disabled属性达到设置按钮为可用状态的目的。

                theShowInput.removeAttribute("disabled");

            }

            // 设置显示图片按钮的点击事件对应的方法。

            theShowInput.onclick = function(){

                // 首先显示图片。

                //theImg.style.display = defaultDisplayType;

                theImg.src = imgSrc;

                // 随后把隐藏图片按钮设为可用。

                theHideInput.removeAttribute("disabled");

                // 设置显示图片按钮为不可用状态。

                this.disabled = "true";

            }

        }


        // 在页面尾部添加一图片

        function addOneImagAtLast(obj){

            var p = document.getElementById("bodyId");

            p.innerHTML = p.innerHTML + "<img src='dragon.png' width='50' height='50' onload='theLittleImageLoaded()'>";

        }


        

        function theLittleImageLoaded(){

            alert("theLittleImageLoaded()");

        }

        

        function theBigImageLoaded(obj){

            // 此处若弹出提示框,则点击显示图片按钮后,会弹出下面的提示框,但是,按钮的离焦事件就不能顺利执行(MACGoole浏览器测试)。

            //alert("theBigImageLoaded(obj) + "+obj);

            var theImg = document.getElementById("imgId");

            theImg.style.backgroundColor = "blue";

        }


        function onfocusFunc(obj){

            //在这里不能弹出提示框,否则在MacGoogle浏览器上会像死循环一样弹出提示框。

            //alert("onfocusFunc");

            obj.style.backgroundColor = "red";

            var theImg = document.getElementById("imgId");

            theImg.style.backgroundColor = "white";

        }

        function onblurFunc(obj){

            //此处可以弹出提示框,不会像上面一样死循环。

            alert("onblurFunc");

            obj.style.backgroundColor = "";

        }

    </script>


    <body onload="initHideAndShowButton()"  id="bodyId">


        <div>

            <div style="display: inline">

                <img src="dragon.png" align= "center" id = "imgId" onload="theBigImageLoaded(this)" width="200px" height="200px">

            </div>

            

        

            <div  style="display: inline-block">

                <form>

                    <input type="button" value="隐藏图片" id="hideImgButton"onfocus="onfocusFunc(this)" onblur="onblurFunc(this)">

                    <input type="button" value="显示图片" id="showImgButton" onfocus="onfocusFunc(this)" onblur="onblurFunc(this)">

                    

                    <input type="button" onclick="addOneImagAtLast(this)">

                </form>

            </div>

        </div>

    </body>    

</html>

0 0