简体   繁体   中英

I have a problem with adding elements to my page after click - “Undefined”, what I need to do?

I am learning JS and I don't know what to do with this problem. A function must return something? If yes, what does it need to return?

It is a part of my 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;};

It is must look like this (without undefined):

screenshotOfGoodVariant

But it looks awful with undefined:

screenshotOfBadVariant

I can not give you html code, because Stack Overflow is warning that there is too much code.

Calling function:

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 = ...”,所以可以!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM