gpt4 book ai didi

javascript - 您如何动态捕获 ID 并更改内容?

转载 作者:行者123 更新时间:2023-11-29 16:01:20 26 4
gpt4 key购买 nike

单击按钮后,我试图更改从后端动态生成的内容。我正在选择按钮 var testOne = document.getElementById("promo_popup_cta_0");这是一个一个动态生成的。只有最后一个数字从 0 开始变化。单击此 promo_popup_cta_0 后,它应该更改 HTML 中的内容并将相同的数字添加到此 ID promo_popup_wrapper_NUMBER

var modalOne = document.getElementById("promo_popup_wrapper_0");
var modalTwo = document.getElementById("promo_popup_wrapper_1");
var trigger = document.querySelector(".popupLink");
var testOne = document.getElementById("promo_popup_cta_0");
var testTwo = document.getElementById("promo_popup_cta_1");
var closeButton = document.querySelector(".close-button");

function toggleModal() {
modalOne.classList.toggle("show-modal-promo");
}

function windowOnClick(event) {
if (event.target === modalOne) {
toggleModal();
}
}

function toggleModalTwo() {
modalOne.classList.toggle("show-modal-promo");
}

function windowOnClick(event) {
if (event.target === modalTwo) {
toggleModal();
}
}

testOne.addEventListener("click", toggleModal);
testTwo.addEventListener("click", toggleModalTwo);

closeButton.addEventListener("click", toggleModal);
window.addEventListener("click", windowOnClick);

})
.modal-promo {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
opacity: 0;
visibility: hidden;
transform: scale(1.1);
transition: visibility 0s linear 0.25s, opacity 0.25s 0s, transform 0.25s;
}
.modal-promo .modal-content-promo {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
padding: 1rem 1.5rem;
width: 24rem;
border-radius: 0.5rem;
}
.modal-promo .modal-content-promo .close-button {
float: right;
width: 1.5rem;
line-height: 1.5rem;
text-align: center;
cursor: pointer;
border-radius: 0.25rem;
background-color: lightgray;
}
.modal-promo .modal-content-promo .close-button:hover {
background-color: darkgray;
}
.show-modal-promo {
opacity: 1;
visibility: visible;
transform: scale(1);
transition: visibility 0s linear 0s, opacity 0.25s 0s, transform 0.25s;
}
<div class="modal-promo" id="promo_popup_wrapper_<?php print $key;?>">
<div class="modal-content-promo">
<span class="close-button">×</span>
<p>
<?php
if (!empty($promo_banner_fields['promo_popup_text'])):
print $promo_banner_fields['promo_popup_text'];
endif;
?>
</p>
</div>
</div>

<u class="popupLink" id="promo_popup_cta_0">One</u>
<u class="popupLink" id="promo_popup_cta_1">Two</u>

让我知道我该怎么做。

最佳答案

试试这个:

  

var modal = document.querySelector(".modal-promo");
var trigger = document.querySelector(".popupLink");
var testOne = document.getElementById("promo_popup_cta_0");
var testTwo = document.getElementById("promo_popup_cta_1");
var closeButton = document.querySelector(".close-button");

function toggleModal(id = null) {
modal.classList.toggle("show-modal-promo");
if (id !==null) {
modal.setAttribute('id', 'promo_popup_wrapper_'+id);
}

}

function windowOnClick(event) {
if (event.target === modal) {
toggleModal();
console.log(event);
}
}

testOne.addEventListener("click", function(event){ toggleModal(event.srcElement.attributes["data-id"].nodeValue); });
testTwo.addEventListener("click", function(event){ toggleModal(event.srcElement.attributes["data-id"].nodeValue); });
closeButton.addEventListener("click", function(){ toggleModal(); });
.modal-promo {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
opacity: 0;
visibility: hidden;
transform: scale(1.1);
transition: visibility 0s linear 0.25s, opacity 0.25s 0s, transform 0.25s;
}
.modal-promo .modal-content-promo {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
padding: 1rem 1.5rem;
width: 24rem;
border-radius: 0.5rem;
}
.modal-promo .modal-content-promo .close-button {
float: right;
width: 1.5rem;
line-height: 1.5rem;
text-align: center;
cursor: pointer;
border-radius: 0.25rem;
background-color: lightgray;
}
.modal-promo .modal-content-promo .close-button:hover {
background-color: darkgray;
}
.show-modal-promo {
opacity: 1;
visibility: visible;
transform: scale(1);
transition: visibility 0s linear 0s, opacity 0.25s 0s, transform 0.25s;
}
<?php 
$key = -1;
$promo_banner_fields['promo_popup_text'] = "Some text";
?>
<div class="modal-promo" id="promo_popup_wrapper_<?= $key;?>">
<div class="modal-content-promo">
<span class="close-button">×</span>
<p>
<?php
if (!empty($promo_banner_fields['promo_popup_text'])):
print $promo_banner_fields['promo_popup_text'];
endif;
?>
</p>
</div>
</div>

<u class="popupLink" id="promo_popup_cta_0" data-id="0">One</u>
<u class="popupLink" id="promo_popup_cta_1" data-id="1">Two</u>

关于javascript - 您如何动态捕获 ID 并更改内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52868921/

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