进阶任务9-事件

2017-07-25  本文已影响0人  RookieD
4bc08396-78b0-48e3-a8bb-f846e86e9d73.jpg 8ddedb2e-f55e-4872-bd36-79c44b71d3f1.jpg
<!doctype html>
<html>
    <head>
        <style>
            .outside{
                width: 100px;
                height: 100px;
                background: red;
            }

            .inside{
                width: 50px;
                height: 50px;
                background: blue;
            }
        </style>
    </head>
    <body>
        <div class="outside">
            <div class="inside"></div>
            </div>
        <script>
            var a = document.querySelector(".inside")
            var b = document.querySelector(".outside")
            a.addEventListener("click", function(e){
                e.stopPropagation()
            })
            b.addEventListener("click", function(){
                console.log("outside")
            }) 
        </script>
    </body>
</html>
<!doctype html>
<html>
    <head>
    </head>
    <body>
        <a href="http://www.sina.com.cn">XinLang</a>
        <script>
            var a = document.querySelector("a")
            a.addEventListener("click", function(e) {
                e.preventDefault()
            })
        </script>
    </body>
</html>
<!doctype html>
<html>
    <head>
    </head>
    <body>
        <ul class="ct">
            <li>这里是</li>
            <li>饥人谷</li>
            <li>前端6班</li>
        </ul>
        <script>
            var a = document.querySelectorAll(".ct>li")
            
            a.forEach(function(e) {
                e.addEventListener("click", function(e) {
                    console.log(this.innerText)
                })
            })
        </script>
    </body>
</html>
<!doctype html>
<html>
    <head>
    </head>
    <body>
        <ul class="ct">
            <li>这里是</li>
            <li>饥人谷</li>
            <li>前端6班</li>
        </ul>
        <input class="ipt-add-content" placeholder="添加内容"/>
        <button id="btn-add-start">开头添加</button>
        <button id="btn-add-end">结尾添加</button>
        <script>
            var list = document.querySelector(".ct")
            var tab = document.querySelector(".ipt-add-content")
            var btn1 = document.querySelector("#btn-add-start")
            var btn2 = document.querySelector("#btn-add-end")
            
            btn1.addEventListener("click", function() {
                var node = document.createElement("li")
                if(tab.value) {
                    node.innerText = tab.value
                    list.insertBefore(node, list.firstChild)
                } 
                
            })

            btn2.addEventListener("click", function() {
                var node = document.createElement("li")
                if(tab.value) {
                    node.innerText = tab.value
                    list.appendChild(node)
                } 
            })
            
        </script>
    </body>
</html>
<!doctype html>
<html>
    <head>
    </head>
    <body>
        <ul class="ct">
            <li data-img="1.png">鼠标放置查看图片1</li>
            <li data-img="2.png">鼠标放置查看图片2</li>
            <li data-img="3.png">鼠标放置查看图片3</li>
        </ul>
        <div class="img-preview"></div>
        <script>
            var node = document.querySelectorAll(".ct>li")
            var show = document.querySelector(".img-preview")

            node.forEach(function(e) {
                e.addEventListener("mouseenter", function(e) {
                    var pic = this.getAttribute("data-img")
                    var node = document.createElement("img")
                    node.setAttribute("src", pic)
                    node.setAttribute("alt", pic)
                    show.appendChild(node)
                })

                e.addEventListener("mouseleave", function(e) {
                    if(show.firstChild) {
                        show.removeChild(show.firstChild)
                    }
                })
            })
        </script>
    </body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读