【问题标题】:Dynamically build a page with JavaScript使用 JavaScript 动态构建页面
【发布时间】:2021-11-14 21:03:26
【问题描述】:

我有 2 张 HTML 图像。

我想在每个图像下动态添加一个JavaScript按钮

这个想法是使用:

  • querySelectorAll
  • for 循环
  • createElement(按钮)到按钮
  • 添加 addEventListener
  • 追加子项

代码:

<h2>Create a new site</h2>
<p>Site – Manage Sites - New:</p>
<dd><img class="screenshot" width="238" height="222" src="https://picsum.photos/200" alt="screenshot" /></dd>
<p>“What would you like to call the website?”</p>
<dd><img class="screenshot" width="238" height="222" src="https://picsum.photos/300" alt="screenshot" /></dd>

img.screenshot {
  margin-bottom: 20px;
  margin-top: 20px;
}

.btn-styled {
  font-size: 14px;
  margin: 10px;
  padding: 0 5px;
  line-height: 2;
}


var eScreens = document.querySelectorAll('img.screenshot');

for (var i = 0; i <= eScreens.length; i++) {
    var button = document.createElement("button");
    button.id = "myButton";
    button.innerHTML = "Show";
    button.className = "btn-styled";
    button.style.background = "orange";
    button.addEventListener("click", function() {
    });
    var dd = document.getElementsByTagName("dd")[0];

    dd.appendChild(button);
}

【问题讨论】:

  • 你忘了问问题。
  • 关于动态显示/隐藏图像
  • 您的问题是关于动态显示/隐藏图像?你的问题是什么? “关于动态显示/隐藏图像”显然不是问题。
  • 请求并不总是以问号结尾的短语。
  • 请阅读tour“Stack Overflow 是一个面向专业和狂热程序员的问题和解答网站。” 用户提出特定的编程问题并获得特定的答案。免费代码编写服务的请求不是特定的编程问题。

标签: javascript html css button


【解决方案1】:

我会将每个图像包装在 figure 元素中,这样每个图像都包含一个图像,当您添加它时,还有一个按钮。

// Cache the figures
const figures = document.querySelectorAll('figure');

// For each add a button, and attach an event listener
figures.forEach(figure => {
  addButton(figure);
  figure.addEventListener('click', handleClick, false);
});

// Create a button, and add it to the end of
// each figure element
function addButton(figure) {
  const button = '<button>Hide</button>';
  figure.insertAdjacentHTML('beforeend', button);
}

// Because the listener is attached to the figure
// we need to check if the element we clicked was the
// button. We can then get the image, and then
// use a class to toggle its visibility
// and update the text in the button
function handleClick(e) {
  const { nodeName, parentNode, textContent } = e.target;
  if (nodeName === 'BUTTON') {
   const image = parentNode.querySelector('img');
   image.classList.toggle('hide');
   e.target.textContent = textContent === 'Hide' 
     ? 'Show'
     : 'Hide';
  }
}
figure { display: inline-block; margin: 0; }
button { display: block; margin: 0 auto; }
.hide { visibility: hidden; }
<figure>
  <img src="https://dummyimage.com/100x100/ffaaaa/000" />
</figure>
<figure>
  <img src="https://dummyimage.com/100x100/aaffaa/000" />
</figure>
<figure>
  <img src="https://dummyimage.com/100x100/aaaaff/000" />
</figure>

【讨论】:

  • 我怎样才能禁止这个“jabaa”,这样他就不会再看到我的问题了?我注意到他的日常活动是给那些仍然试图从别人的经验中学习一些东西的人投反对票。我认为 Stack Overflow 是一个可以让你学到一些东西的网站,而不是一个社交网站(如 Facebook、Twitter、Instagram 等),每个人都会因为可以而给予减分。
  • 重点是您发表了声明,添加了一些数据,但没有提供任何调试信息或提及您为识别问题所采取的步骤。它更像是“这是我的代码,修复它”。进行简单的调试并浏览代码将帮助您更好地构建您的问题。 @ICXC
猜你喜欢
  • 1970-01-01
  • 2010-11-27
  • 2014-04-28
  • 2017-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多