3 回答
TA贡献1877条经验 获得超6个赞
你不应该使用相同的元素,它可以在这里工作,但它是令人不快的。我用相同的.当然,不会加载。我已经在代码本身中进行了解释。看,如果它对你有用。idclassiframe
const selectedActivity = document.querySelectorAll(".other-activies-link");
selectedActivity.forEach((link) => {
link.addEventListener("click", (event) => { // pass event parameter here
document.getElementById(
"myFrame"
).src = `/files/${event.target.innerText}.pdf`; // use innerText instead
console.clear();
console.log(event.target.innerText);
console.log(event.target.textContent);
});
});
<p><a class="other-activies-link">Semana<br> Omnistack 11</a></p>
<p><a class="other-activies-link">28ª Semana do IME</a></p>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<iframe id="myFrame" frameborder="0" allowfullscreen></iframe>
</div>
</div>
请参阅和此处之间的区别。innerText
textContent
TA贡献1799条经验 获得超8个赞
您应该改用类,元素的id应始终是唯一的
<p><a class="other-activies-link">Semana Omnistack 11</a></p>
<p><a class="other-activies-link">28ª Semana do IME</a></p>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<iframe id="myFrame" frameborder="0" allowfullscreen></iframe>
</div>
</div>
这应该有效,
const selectedActivity = document.querySelectorAll(".other-activies-link");
...
添加回答
举报