Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 你好我尝试单击div的所有元素,但它现在可以工作了_Javascript_Svg - Fatal编程技术网

Javascript 你好我尝试单击div的所有元素,但它现在可以工作了

Javascript 你好我尝试单击div的所有元素,但它现在可以工作了,javascript,svg,Javascript,Svg,我试着点击所有的div,但最后一个div事件有效。如何为每个div调用函数? 我想知道哪个div被点击了,这个函数的javascript调用 <svg height="300" width="300" class="centerization"> <style> .es{ margin-top: 30px; } .es:hover { cursor: pointer;

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

<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]);
    });
};


埃斯{
边缘顶部:30px;
}
.es:悬停{
光标:指针;
}
var svg=document.getElementsByClassName(“es”);
for(svg中的var属性){
svg[property].addEventListener(“单击”,函数()){
警报(“单击”+svg[属性]);
});
};

您需要将
单击
事件添加到
svg
数组的每个元素中。您可以为此使用简单的
for
循环:

var svg=document.getElementsByClassName(“es”);

对于(var i=0;i < p>),GETelEntsB**方法返回HTMLCype,这可能是很难处理的。考虑使用QueRealStudioRoad,它返回静态的NODLIST——不像HTMLCype,它可以直接迭代,在迭代时不会改变,而且更灵活。

document.querySelectorAll('.es'))
.forEach((es)=>{
es.addEventListener('click',()=>alert('clicked'+es.outerHTML));
});
.es{
边缘顶部:30px;
}
.es:悬停{
光标:指针;
}


你的js不在
标签下。我担心这在第一个标签中是否有效place@GeorgeBailey我认为这仅仅是一个如此后格式化的问题@გენომუმლაძე 单击所有框,您将收到无法理解的警报。结果仅适用于最后一个DIV@გენომუმლაძე 阅读此
我尝试单击所有div,但事件对最后一个div有效。如何为每个div调用函数?
您将知道谁不懂。您看到此
…为每个div调用函数