我对JavaScript还是很陌生。不好意思我有一些HTML和一个Velocity Template Engine #foreach语句,可以从数据库中渲染一些html。
这个想法是将鼠标悬停在渲染的图像上并获得结束。我遇到的问题是(我认为)JavaScript,只有第一个渲染的图像实际上在获取覆盖。出于某种原因,第一个之后都不是。
$(document).ready(function(){
var amiibo = document.getElementById("amiibo");
var activeAmiiboID = amiibo.textContent || amiibo.innerText;
$("#inactive-"+activeAmiiboID).mouseover(function() {
console.log("#inactive has been moused over / activeAmiiboID: "+ activeAmiiboID);
$("#active-"+activeAmiiboID).removeClass("is-invisible");
});
这是HTML代码段:
<div class="columns is-multiline">
<!-- Here are all of the Amiibo -->
#foreach( $amiibo in $amiibos )
<div class="column is-one-fifth">
<div class="amiibo-id is-hidden" id="amiibo">$amiibo.AmiiboID</div>
<article class="message is-large is-danger">
<div class="message-header">
<p></p>
<p>$amiibo.Name</p>
<p></p>
</div>
<center> <!-- Amiibo -->
<figure class="image has-text-light">
<img class="amiibo-image" src="$amiibo.ImageURL" id="inactive-$amiibo.AmiiboID">
<!-- Content Over Amiibo -->
<figcaption class="overlay is-overlay is-invisible" id="active-$amiibo.AmiiboID">
<!-- Background -->
<img class="background is-overlay" alt="Mario" src="https://storage.googleapis.com/amiibo/black.png">
<div style="height:16px;font-size:16px;"> </div>
<!-- Buttons -->
<div class="tile is-ancestor">
<!-- Collected "Mine" (Star) Button -->
<div class="tile is-parent">
<article class="tile is-child">
<div class="star">
<label>
<span class="fa-stack">
<i class="fas fa-star fa-2x"></i>
<h4 class="subtitle is-5 has-text-light is-bold">Mine</h4>
<input class="mine is-hidden" type="checkbox" id="mine">
</span>
</label>
</div>
</article>
</div>
<!-- Collected "Love" (Heart) Button -->
<div class="tile is-parent">
<article class="tile is-child">
<div class="heart">
<label>
<span class="fa-stack">
<i class="fas fa-heart fa-2x"></i>
<h4 class="subtitle is-5 has-text-light is-bold">Love</h4>
<input class="love is-hidden" type="checkbox" id="love">
</span>
</label>
</div>
</article>
</div>
<!-- Collected "Want" (Gem) Button -->
<div class="tile is-parent">
<article class="tile is-child">
<div class="gem">
<label>
<span class="fa-stack">
<i class="fas fa-gem fa-2x"></i>
<h4 class="subtitle is-5 has-text-light is-bold">Want</h4>
<input class="want is-hidden" type="checkbox" id="want">
</span>
</label>
</div>
</article>
</div>
</figcaption>
</figure>
</center>
</article>
</div>
#end
</div>
我在做什么错?任何帮助或指导,我们将不胜感激。
Travis W。
答案 0 :(得分:0)
当前结果是预期的,因为现在您仅使用当前activeAmiiboID定位图像。您应按此类将所有图像作为目标,以获取所需的功能:
$(document).ready(function(){
$('.amiibo-image').mouseover(function() {
var imgId = $(this).attr('id');
var id = imgId.split('-')[1];
$("#active-"+ id).removeClass("is-invisible");
});
});