zoukankan      html  css  js  c++  java
  • JS怎么判断是哪个元素触发了点击事件?

    • 点击图形2和5,输出他们的名称和改变他们的颜色
    <!DOCTYPE html>
    <head>
        <meta charset="UTF-8">
        <title>JS怎么判断是哪个元素触发了点击事件?</title>
        <style>
            .smallDiv{
                border:1px solid #cccccc;
                50px; height:50px;
                margin: 10px;
                background:#ccc;
                display: -webkit-inline-box;
            }
        </style>
    </head>
    <body>
    
    <div style="auto;background-color: #1A68A2" id="bigBox">
        <div class="smallDiv" data-index="1">1</div>
        <div class="smallDiv" data-index="2">2</div>
        <div class="smallDiv" data-index="3">3</div>
        <div class="smallDiv" data-index="4">4</div>
        <div class="smallDiv" data-index="5">5</div>
        <div class="smallDiv" data-index="6">6</div>
        <div class="smallDiv" data-index="7">7</div>
        <div class="smallDiv" data-index="8">8</div>
    </div>
    <script>
        function processInnderDiv(domDiv){
            console.log('div[data-index=%s] click',domDiv.getAttribute("data-index"));//输出被点击对象的序号
            domDiv.style="background-color:red;"//改变被点击对象的颜色
        }
    
        document.getElementById("bigBox").addEventListener("click",function(event){
            processInnderDiv(event.target);
        },false);
    </script>
    </body>
    </html>
    

    742485425 复制数字打开支付宝免费领红包

  • 相关阅读:
    learnyou 相关网站
    hdu 3038 How Many Answers Are Wrong
    hdu 3047 Zjnu Stadium 并查集高级应用
    poj 1703 Find them, Catch them
    poj 1182 食物链 (带关系的并查集)
    hdu 1233 还是畅通工程
    hdu 1325 Is It A Tree?
    hdu 1856 More is better
    hdu 1272 小希的迷宫
    POJ – 2524 Ubiquitous Religions
  • 原文地址:https://www.cnblogs.com/MrFlySand/p/15614658.html
Copyright © 2011-2022 走看看