二阶段day8-DOM

一、增删子标签

增删标签
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>增删标签</title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
            #container {
                margin: 20px 50px;
            }
            #fruits li {
                list-style: none;
                width: 200px;
                height: 50px;
                font-size: 20px;
                line-height: 50px;
                background-color: cadetblue;
                color: white;
                text-align: center;
                margin: 2px 0;
            }
            #fruits>li>a {
                float: right;
                text-decoration: none;
                color: white;
                position: relative;
                right: 5px;
            }
            #fruits~input {
                border: none;
                outline: none;
                font-size: 18px;
            }
            #fruits~input[type=text] {
                border-bottom: 1px solid darkgray;
                width: 200px;
                height: 50px;
                text-align: center;
            }
            #fruits~input[type=button] {
                width: 80px;
                height: 30px;
                background-color: coral;
                color: white;
                vertical-align: bottom;
                cursor: pointer;
            }
        </style>
    </head>
    <body>
        <div id="container">
            <ul id="fruits">
                <li>苹果<a href="javascript:void(0)">×</a></li>
                <li>香蕉<a href="javascript:void(0)">×</a></li>
                <li>火龙果<a href="javascript:void(0)">×</a></li>
                <li>西瓜<a href="javascript:void(0)">×</a></li>
                <!--a标签有默认的跳转页面的行为,有两种方法可以阻止其行为;-->
                <!--1、让a标签的href="javascript:void(0)"-->
            </ul>
            <input type="text" name="fruit">
            <input id="ok" type="button" value="确定">
        </div>
        <script src="day8/web1807/js/mylib.js">
        </script>
        <script >
            //删除子标签
            var anchors=document.querySelectorAll('#fruits a')
            for (var i=0;i<anchors.length;i+=1) {
                anchors[i].addEventListener('click',function(evt){
                    //2、evt.preventDefault()——》阻止事件的默认行为,非属性;
                    evt.preventDefault()
                    var a=evt.target || evt.srcElement  
                    var li=a.parentNode
                    li.parentNode.removeChild(li)
                    
                })
            }
            
            //添加子标签
            var ul=document.getElementById('fruits')
            var okButton=document.querySelector('#ok')
            okButton.addEventListener('click',function(){
                var input=document.querySelector('#container input[type=text]')
                var fruitName=input.value.trim() //trim()清除内容首尾空格符
                if (fruitName.length>0){
                    var li=document.createElement('li')    //创建标签新li
                    li.innerHTML=fruitName
                    li.style.backgroundColor=randomColor()
                    var a =document.createElement('a')
                    a.href=''
                    a.textContent='×'
                    li.appendChild(a)
                    ul.insertBefore(li,ul.firstChild) //为父标签ul添加子标签li,其位置在第一个子元素之前
                }
                input.value=''  //添加完元素后,清空文本框
                input.focus()   //将文本框设置为焦点
            })
            
            
        </script>
    </body>
</html>

二、增添+闪烁(子标签)

增添+闪烁
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            #container {
                width: 800px;
                height: 400px;
                margin: 10px auto;
                border: 1px solid black;
                overflow: hidden;
            }
            #buttons {
                width: 800px;
                margin: 10px auto;
                text-align: center;
            }
            #add, #fla {
                border: none;
                outline: none;
                width: 80px;
                height: 30px;
                background-color: red;
                color: white;
                font-size: 16px;
                cursor: pointer;
            }
            .small {
                width: 80px;
                height: 80px;
                float: left;
            }
        </style>
    </head>
    <body>
        <div id="container"></div>
        <div id="buttons">
            <button id="add">添加</button>
            <button id="fla">闪烁</button>
        </div>
        <script src="js/mylib.js"></script>
        <script>
            //添加
            var bigDiv = document.querySelector('#container');
            var addButton = document.querySelector('#add');
            addButton.addEventListener('click', function() {
                var smallDiv = document.createElement('div');
                 smallDiv.className = 'small';
                // smallDiv.style.width = '80px';
                // smallDiv.style.height = '80px';
                // smallDiv.style.float = 'left';
                smallDiv.style.backgroundColor = randomColor();
                bigDiv.insertBefore(smallDiv, bigDiv.firstChild);
            });
            
            
            //闪烁
            var flaButton = document.querySelector('#fla');
            var isFlashing = false;
            var timerId;
            flaButton.addEventListener('click', function(evt) {
                isFlashing = !isFlashing;
                if (isFlashing) {
                    timerId = window.setInterval(function() {
                        var divs = document.querySelectorAll('#container>div');
                        for (var i = 0; i < divs.length; i += 1) {
                            divs[i].style.backgroundColor = randomColor();
                        }
                    }, 200);
                    flaButton.textContent = '暂停';
                } else {
                    window.clearInterval(timerId);
                    flaButton.textContent = '闪烁';
                }
            });
        </script>
    </body>
</html>

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • HTML标签解释大全 一、HTML标记 标签:!DOCTYPE 说明:指定了 HTML 文档遵循的文档类型定义(D...
    米塔塔阅读 3,648评论 1赞 41
  • 水木然的《时代之巅:互联网构建新经济》第三节:财富和货币 “资本”是一种具备天然的扩张性、侵略性的东...
    Jane_102阅读 319评论 0赞 0
  • 我能想到最浪漫的事就是和你睡觉 是早晨你的早安和夜里你的晚安。我们相拥而睡,让太阳陪我们一起醒来,你的刷牙洗发我负...
    Thesusan阅读 218评论 0赞 0

友情链接更多精彩内容