gpt4 book ai didi

javascript - 当元素具有相同的类时如何分配数据?

转载 作者:行者123 更新时间:2023-11-28 15:38:54 25 4
gpt4 key购买 nike

我一直面临一个小问题..这是关于类和 ID

假设我有一个包含 5 个段落的 2 div,所有段落都代表相同的内容。所以第一段代表时间,第二段代表天气等等。

现在想象一下,当鼠标悬停在将显示所有段落。

如果您想根据鼠标悬停的值更改这些值,您将如何将数据分配给该特定类?看到这很糟糕..因为 Id 是唯一的

<a href="javascript:void(0)" id="headLeftSlot" class="itemSlot">
<div class="headSlot">
<div class="itemImage" id="headSlot">

</div>
<section>
<span class="tooltiptext">
<h3 id="itemInfoName">Item Name</h3>
<h4>Attack bonuses</h4>
<p id="aStab">Stab: +5</p>
<p id="aSlash">Slash: +5</p>
<p id="aCrush">Crush: +5</p>
<p id="aMagic">Range: +5</p>
<p id="aRange">Range: +5</p>

<h4>Defence bonuses</h4>
<p id="dStab">Stab: +5</p>
<p id="dSlash">Stab: +5</p>
<p id="dCrush">Stab: +5</p>
</span>
</section>
</div>
</a>


<div class="topItems">
<a href="javascript:void(0)" id="capeLeftSlot" class="itemSlot">

<section class="capeSlot">
<div class="itemImage" id="capeSlot">

</div>
<span class="tooltiptext">
<h3 id="itemInfoName">Item Name</h3>
<h4>Attack bonuses</h4>
<p id="aStab">Stab: +5</p>
<p id="aSlash">Slash: +5</p>
<p id="aCrush">Crush: +5</p>
<p id="aMagic">Range: +5</p>
<p id="aRange">Range: +5</p>

<h4>Defence bonuses</h4>
<p id="dStab">Stab: +5</p>
<p id="dSlash">Stab: +5</p>
<p id="dCrush">Stab: +5</p>
</span>
</section>
</a>

但是如果我为它们分配类而不是 ID,我将如何在悬停时更改第二个 div 的内容?

因为现在我正在这样做,这是不好的。

$('.itemImage').mouseover(function(event){
if(model != null){
document.getElementById("itemInfoName").style.color = "Orange";
document.getElementById("itemInfoName").innerHTML = model.Name;
document.getElementById("aStab").innerHTML = "Stab: " + model.AStab;
document.getElementById("aSlash").innerHTML = "Slash: " + model.ASlash;
document.getElementById("aCrush").innerHTML = "Crush: " + model.ACrush;
document.getElementById("aMagic").innerHTML = "Magic: " + model.AMagic;
document.getElementById("aRange").innerHTML = "Range: " + model.ARange;

document.getElementById("dStab").innerHTML = "Stab: " + model.DStab;
document.getElementById("dSlash").innerHTML = "Slash: " + model.DSlash;
document.getElementById("dCrush").innerHTML = "Crush: " + model.DCrush;
document.getElementById("dMagic").innerHTML = "Magic: " + model.DMagic;
document.getElementById("dRange").innerHTML = "Range: " + model.DRange;

document.getElementById("mStrength").innerHTML = "Melee Strength: " + model.MeleeStrength;
document.getElementById("rStrength").innerHTML = "Ranged Strength: " + model.RangedStrength;
document.getElementById("mDamage").innerHTML = "Magic Damage: " + model.MagicDamage;
document.getElementById("Prayer").innerHTML = "Prayer: " + model.Prayer;

}

});

最佳答案

您可以通过不同的数据属性来识别div。例如,您有两个具有相同类“tooltiptext”的元素

<span class="tooltiptext">

你可以添加一些数据属性来区分它

<span class="tooltiptext" data-some-id="1">

然后您可以通过以下方式选择它:

$(".tooltiptext[data-some-id='1']")

 <a href="javascript:void(0)" id="headLeftSlot" class="itemSlot">
<div class="headSlot">
<div class="itemImage" id="headSlot">

</div>
<section>
<span class="tooltiptext">
<h3 id="itemInfoName">Item Name</h3>
<h4>Attack bonuses</h4>
<p id="aStab">Stab: +5</p>
<p id="aSlash">Slash: +5</p>
<p id="aCrush">Crush: +5</p>
<p id="aMagic">Range: +5</p>
<p id="aRange">Range: +5</p>

<h4>Defence bonuses</h4>
<p id="dStab">Stab: +5</p>
<p id="dSlash">Stab: +5</p>
<p id="dCrush">Stab: +5</p>
</span>
</section>
</div>
</a>


<div class="topItems">
<a href="javascript:void(0)" id="capeLeftSlot" class="itemSlot">

<section class="capeSlot">
<div class="itemImage" id="capeSlot">

</div>
<span class="tooltiptext">
<h3 id="itemInfoName">Item Name</h3>
<h4>Attack bonuses</h4>
<p id="aStab">Stab: +5</p>
<p id="aSlash">Slash: +5</p>
<p id="aCrush">Crush: +5</p>
<p id="aMagic">Range: +5</p>
<p id="aRange">Range: +5</p>

<h4>Defence bonuses</h4>
<p id="dStab">Stab: +5</p>
<p id="dSlash">Stab: +5</p>
<p id="dCrush">Stab: +5</p>
</span>
</section>
</a>

$(".tooltiptext").on("mouseover", function(e){
var el = $(e.currentTarget);
$("#tip").html(el.data("tip"));
});


$(".tooltiptext").on("mouseout", function(e){
var el = $(e.currentTarget);
$("#tip").html("");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="tip"></div>

<a href="javascript:void(0)" id="headLeftSlot" class="itemSlot">
<div class="headSlot">
<div class="itemImage" id="headSlot">

</div>
<section>
<span class="tooltiptext" data-tip="my content1">
<h3 id="itemInfoName">Item Name</h3>
<h4>Attack bonuses</h4>
<p id="aStab">Stab: +5</p>
<p id="aSlash">Slash: +5</p>
<p id="aCrush">Crush: +5</p>
<p id="aMagic">Range: +5</p>
<p id="aRange">Range: +5</p>

<h4>Defence bonuses</h4>
<p id="dStab">Stab: +5</p>
<p id="dSlash">Stab: +5</p>
<p id="dCrush">Stab: +5</p>
</span>
</section>
</div>
</a>


<div class="topItems">
<a href="javascript:void(0)" id="capeLeftSlot" class="itemSlot">

<section class="capeSlot">
<div class="itemImage" id="capeSlot">

</div>
<span class="tooltiptext" data-tip="my content2">
<h3 id="itemInfoName">Item Name</h3>
<h4>Attack bonuses</h4>
<p id="aStab">Stab: +5</p>
<p id="aSlash">Slash: +5</p>
<p id="aCrush">Crush: +5</p>
<p id="aMagic">Range: +5</p>
<p id="aRange">Range: +5</p>

<h4>Defence bonuses</h4>
<p id="dStab">Stab: +5</p>
<p id="dSlash">Stab: +5</p>
<p id="dCrush">Stab: +5</p>
</span>
</section>
</a>

关于javascript - 当元素具有相同的类时如何分配数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52811829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com