javascript - 你好。我尝试单击 div 的所有元素,但现在可以了

标签 javascript svg

我尝试单击所有 div,但事件适用于最后一个 div。如何为每个div调用函数? 我想知道哪个 div 被点击了 javascript 调用函数。

/image/eUhjZ.jpg

<svg height="300" width="300" class="centerization">
    <style>
        .es{
            margin-top: 30px;
        }
        .es:hover {
            cursor: pointer;
        }
    </style>
    <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es"> 
    </rect>
    <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" 
    x="100"></rect>
    <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" 
    x="200"></rect>
    <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" 
    y="100"></rect>
    <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" 
    y="100" x="100"></rect>
    <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" 
    y="100" x="200"></rect>
    <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" 
    y="200"></rect>
    <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" 
    y="200" x="100"></rect>
    <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" 
    y="200" x="200"></rect>
</svg>

var svg = document.getElementsByClassName("es");
for (var property in svg) {
    svg[property].addEventListener("click",function(){
        alert("clicked"+svg[property]);
    });
};

最佳答案

getElementsBy* 方法返回 HTMLCollections,这可能很难使用。考虑使用 querySelectorAll,它返回一个静态 NodeList - 与 HTMLCollection 不同,它可以直接迭代,迭代时不会改变,而且更加灵活。

document.querySelectorAll('.es')
  .forEach((es) => {
    es.addEventListener('click', () => alert('clicked ' + es.outerHTML));
  });
.es {
  margin-top: 30px;
}

.es:hover {
  cursor: pointer;
}
<svg>
<rect height="40" width="80" fill="blue" rx="5" ry="5" class="es">
</rect>
<rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" x="100"></rect>
<rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" x="200"></rect>
<rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="100"></rect>
<rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="100" x="100"></rect>
<rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="100" x="200"></rect>
<rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="200"></rect>
<rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="200" x="100"></rect>
<rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="200" x="200"></rect>
</svg>

关于javascript - 你好。我尝试单击 div 的所有元素,但现在可以了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49937108/

相关文章:

javascript - 如何使用 WWW::Mechanize 抓取具有动态表单的网站?

javascript - HTML 中的 SVG 文件

javascript - iframe 不显示其内容

javascript - Firebase身份验证中的用户ID在所有身份验证提供程序(Google,Facebook,Github)中如何相同?

javascript - React useEffect 导致 : Can't perform a React state update on an unmounted component

javascript - 简单任务(SVG 与 Canvas)

android - 应用程序随机崩溃,致命信号 7 (SIGBUS),代码 2 或致命信号 11 (SIGSEGV),代码 1

javascript - 在 asp.net 中单击按钮时显示 gif,并在函数完成后隐藏

javascript - D3 饼图弧线过渡到 180° 时不可见

html - 在 IE10 中移动线条时,SVG 线条标记不会更新?