gpt4 book ai didi

javascript - 单击后向我的页面添加元素时出现问题 - "Undefined",我需要做什么?

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

我是学JS的,遇到这个问题不知道怎么办。一个函数必须返回一些东西?如果是,需要返回什么?

它是我的 js 的一部分:

const workExamplesAdding = (htmlFirstText, htmlSecondText) => {
htmlFirstText += '<img src="/images/your_img_here1.png" alt="Example of our work" class="work-examples__item-img"><div class="work-examples__item-img--overlay"><a href="#" class="work-examples__item-link"><i class="far fa-eye"></i></a></div>';
htmlSecondText += '<img src="/images/your_img_here2.png" alt="Example of our work" class="work-examples__item-img"><div class="work-examples__item-img--overlay"><a href="#" class="work-examples__item-link"><i class="far fa-eye"></i></a></div>';

document.getElementById("work-examples__item--afterClick").innerHTML = htmlFirstText;
document.getElementById("work-examples__item--afterClick2").innerHTML = htmlSecondText;
document.getElementById("work-examples__item--afterClick3").innerHTML = htmlFirstText;
document.getElementById("work-examples__item--afterClick4").innerHTML = htmlSecondText;
document.getElementById("work-examples__item--afterClick5").innerHTML = htmlFirstText;
document.getElementById("work-examples__item--afterClick6").innerHTML = htmlSecondText;
document.getElementById("work-examples__item--afterClick7").innerHTML = htmlFirstText;
document.getElementById("work-examples__item--afterClick8").innerHTML = htmlSecondText;};

它必须看起来像这样(没有未定义):

screenshotOfGoodVariant

但是对于 undefined 看起来很糟糕:

screenshotOfBadVariant

我不能给你 html 代码,因为 Stack Overflow 警告说代码太多。

调用函数:

const btnClick = (elementIsClicked, element) => {
elementIsClicked = false;

function clickHandler() {
if (elementIsClicked = true) {
showText();
workExamplesAdding();
} else {
alert("Error");
};
};

// grab a reference to your element
element = document.getElementById('load-work-examples-btn');
// associate the function above with the click event
element.addEventListener('click', clickHandler);
}

最佳答案

哇,问题出在“htmlFirstText += ...”中,我执行了“htmlFirstText = ...”,它成功了!

关于javascript - 单击后向我的页面添加元素时出现问题 - "Undefined",我需要做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54972974/

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