Adding close button in div to close the box

后端 未结 7 1909
一整个雨季
一整个雨季 2020-12-23 20:13

I have created a URL preview box for the entered URL.

Preview is shown in the div box. I want to add a close option on the right top. How could be done so that when u

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-23 20:55

    Most simple way (assumed you want to remove the element)

    x
    

    Add this inside your div, an example here.

    You may also use something like this

    window.onload = function(){
        document.getElementById('close').onclick = function(){
            this.parentNode.parentNode.parentNode
            .removeChild(this.parentNode.parentNode);
            return false;
        };
    };
    

    An Example Here.

    Css for close button

    #close {
        float:right;
        display:inline-block;
        padding:2px 5px;
        background:#ccc;
    }
    

    You may add a hover effect like

    #close:hover {
        float:right;
        display:inline-block;
        padding:2px 5px;
        background:#ccc;
        color:#fff;
    }
    

    Something like this one.

提交回复
热议问题