【发布时间】:2018-02-01 04:40:28
【问题描述】:
我的脚本有问题。
http://danny.b.cmmstudents.nl/MM-4/pages/hardware.html
这是我正在使用的 javascript:
var imgPad = "../media/images/";
var canSwap = document.images ? true : false;
var mapAreas = document.getElementsByClassName('iphoneLink');
var eventList = ['mouseover', 'mouseout', 'click', 'touchstart'];
var map = document.getElementById('softwareMap');
var informatieHeading = document.getElementById('informatieHeading');
var informatiePar = document.getElementById('informatiePar');
for(var i=0; i<mapAreas.length; i++){
for(theEvent of eventList){
mapAreas[i].addEventListener(theEvent, function(e){
if(e.type == 'mouseover'){
if(map.parentNode.id == "softwareMap"){
var checkDb = hwItems[this.getAttribute("data-dbId")].afbeelding;
swapImage("mainImg", checkDb); // Perform function
} else{
var checkDb = swItems[this.getAttribute("data-dbId")].afbeelding;
swapImage("mainImg", checkDb); // Perform function
}
} else if (e.type == 'mouseout'){
if(map.parentNode.id == "softwareMap"){
swapImage("mainImg", 'iphonex.png'); // Perform function
} else{
swapImage("mainImg", 'iphonexInside.png'); // Perform function
}
} else if(e.type == 'click'){
if(map.parentNode.id == "softwareMap"){
informatieHeading.textContent = swItems[this.getAttribute("data-dbId")].titel;
informatiePar.textContent = swItems[this.getAttribute("data-dbId")].omschrijving;
} else{
informatieHeading.textContent = hwItems[this.getAttribute("data-dbId")].titel;
informatiePar.textContent = hwItems[this.getAttribute("data-dbId")].omschrijving;
}
} else if (e.type == 'touchstart'){
informatieHeading.textContent = hwItems[this.getAttribute("data-dbId")].titel;
informatiePar.textContent = hwItems[this.getAttribute("data-dbId")].omschrijving;
};
});
}
}
这是错误:
未捕获的类型错误:无法读取 null 的属性“parentNode” 在 HTMLAreaElement。
这是硬件页面的 HTML: softwarepage 也一样,但有 id 'softwareMap'
<main id="content">
<div class="iphoneTekst">
<p>Klik met je vinger op de interactieve iPhone</p>
<p>Ga met de muis over de interactieve iPhone</p>
</div>
<div id="iphone"><img id="mainImg" src="../media/images/iphonexInside.png" alt="iPhone X" usemap="#hardwareMap" width="772 " height="1472"></div>
<map id="hardwareMap" name="hardwareMap"><!-- image Map -->
<area class="iphoneLink" data-dbId="0" alt="" title="" href="#" shape="rect" coords="570,60,706,325">
<area class="iphoneLink" data-dbId="1" alt="" title="" href="#" shape="rect" coords="59,1209,347,1316">
<area class="iphoneLink" data-dbId="2" alt="" title="" href="#" shape="rect" coords="498,34,550,114">
<area class="iphoneLink" data-dbId="3" alt="" title="" href="#" shape="rect" coords="449,1358,678,1433">
<area class="iphoneLink" data-dbId="4" alt="" title="" href="#" shape="poly" coords="66,235,417,233,420,829,625,837,626,1201,57,1205">
</map>
<article id="informatie"><!-- Div wordt ingeladen met info van de iphone-database.js -->
<div>
<h2 id="informatieHeading">iPhone X</h2>
<p id="informatiePar">We hebben altijd al een iPhone willen maken die één en al scherm is. Een iPhone waar je zo in opgaat dat je je er helemaal in verliest. Eentje die zo intelligent is dat hij reageert op een tikje, je stem of zelfs je blik. Met iPhone X hebben we dit doel nu bereikt.<br>Welkom in de toekomst.</p>
</div>
</article>
<div class="ctaBackToTop"><a href="#iphone">Back to top</a></div>
</main>
【问题讨论】:
-
softwareMap在您的 HTML 源代码中不存在。 -
请给我们看看html。
-
我正在查看您的链接,我在控制台的任何地方都没有看到 softwareMap。
-
这个问题发布了 OP 声明的 HTML 和 JavaScript 实际上并没有一起使用。在更正问题以显示一起运行的代码之前,这是没有意义的。投票结束。
-
我投票结束这个问题作为题外话,因为 JavaScript 不与 HTML 一起使用。
标签: javascript