gpt4 book ai didi

javascript - 在 jQuery 中为 id 添加一个数字,以便在 jQuery/JavaScript 中使用

转载 作者:行者123 更新时间:2023-11-30 11:17:18 25 4
gpt4 key购买 nike

我想为变量分配一个 id 并将 clicked_id 附加到该 id。然后,我想在 openModal() 函数中使用它。我的想法是这样的:

JS文件

var thismodal = "#myModal";

function openModal(clicked_id) {
var trueModalId = thismodal + clicked_id;
document.getElementById(trueModalId).style.display = "block";
}

HTML

    <img class="hover-shadow cursor card-img-top" src="images/dmc.png" id="1" onclick="openModal(this.id);currentSlide(1)" alt="">
<div id="myModal1" class="modal"></div>

我如何使它工作?

编辑

将有多个 HTML 代码实例。因此,为了使它们独一无二,我将数字分配为 img 的 ID,并将数字添加到 myModal。在上面的实例中,使用了 myModal1,因为 img 的 id 是 1。这就是为什么我不想直接使用 myModal1

最佳答案

试试这个

var thismodal = "#myModal";

function openModal(clicked_id) {
var trueModalId = thismodal + clicked_id;
$(trueModalId).show();
}

关于javascript - 在 jQuery 中为 id 添加一个数字,以便在 jQuery/JavaScript 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51075954/

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