简体   繁体   English

单击“未定义”后,向页面添加元素时出现问题,我该怎么办?

[英]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. 我正在学习JS,但不知道该怎么办。 A function must return something? 一个函数必须返回什么? If yes, what does it need to return? 如果是,它需要返回什么?

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

It is must look like this (without undefined): 它必须看起来像这样(没有未定义):

screenshotOfGoodVariant

But it looks awful with undefined: 但是用undefined看起来很糟糕:

screenshotOfBadVariant

I can not give you html code, because Stack Overflow is warning that there is too much code. 我不能给你html代码,因为Stack Overflow警告说代码太多。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在将对象添加到数组时遇到问题 - I have problem with adding my object to array 为什么刷新页面后立即必须单击带有slideToggle的按钮两次才能使未切换的表单可见? - Why immediately after refreshing my page do I have to click the button with slideToggle on it twice leave the un-toggled form visible? 在脚本标记中执行一些javascript后,为什么在html文件中有此文本“ undefined”? - Why do I have this text “undefined” in my html file after I execute some javascript in script tag? onChange function 一键停止,我需要写什么代码才能无限期地继续? (Javascript) - onChange function stops after one click, what code do I need to write so that it continues indefinitely? (Javascript) 需要第1页的Javascript来操作我在第2页上插入的DIV吗? - Need page 1 Javascript to action a DIV that i have inserted on my page 2? 我需要隐藏“tr”元素的CSS规则,其子元素“td”具有带有空值属性的子“输入”元素? - What CSS rule do I need to hide “tr” elements whose children “td”'s have child “input” elements with empty value attributes? 单击应用程序通知后,如何重定向到我的应用程序配置文件页面? - How do I redirect to my application profile page after I click on the application notification? 当我点击编辑按钮时出现问题,所有其他元素都听点击,但我只需要我点击的那个 - A problem when i click on edit button, all the other elements listen to the click but i need just the one i click on Bootstrap 5:我需要做什么才能在导航栏中有一个搜索图标,当我单击它时,它会在导航栏下方显示一个搜索框? - Bootstrap 5: What I need to do to have a search icon in navbar that when I click it, it will show a search box under the navbar? 当我单击导航栏中的按钮或其他按钮时,它会将我重定向到一个名为 undefined 的页面,但我想要做的是打开一个引导模式 - When I click a button in navbar or other button it redirects me to a page called undefined but what I want to do is open a bootstrap modal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM