【发布时间】:2020-01-23 10:01:55
【问题描述】:
我正在创建地图;我想使用 Array 向每个位置添加一些信息。 例如:我有 3 个 div 和一个具有三个长度的数组(div 的信息)。 现在,每当我将鼠标悬停在 div 上时,它应该将属性 (elID) 与 Array 进行比较,并获取 head 或 Paragraph 等信息。
HTML 示例:
<div id="container">
<div elID = "first">first</div>
<div elID = "second">second</div>
<div elID = "third">third</div>
</div>
我在 Javascript 中做了什么:
const locations = [
{"location": "first", "head":"This is first the head", "paragraph":"kommtNoch"},
{"location": "second", "head":"This is the second head", "paragraph":"kommtNoch"},
{"location": "third", "head":"This is the third head", "paragraph":"kommtNoch"} ]
const red = document.querySelectorAll('#container > div');
for(i=0; i<red.length;i++){
red[i].setAttribute('onmouseover', 'myFirstFunction(this)');
function myFirstFunction(e){
if(e.classList === locations.indexOf()){
console.log(locations[i].location);
}
我在这里找到了一些答案,但我很难理解:
Check if an array contains any element of another array in JavaScript
【问题讨论】:
标签: javascript arrays compare