【发布时间】:2022-08-13 23:18:05
【问题描述】:
以下代码包含 2 个按钮及其各自的下拉内容。 当我单击第一个按钮时,另一个按钮会自行移动。我该如何阻止这种情况发生?
var coll = document.getElementsByClassName(\"button\");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].addEventListener(\"click\", function() {
this.classList.toggle(\"active\");
var content = this.nextElementSibling;
if (content.style.display === \"block\")
content.style.display = \"none\";
else
content.style.display = \"block\";
});
}
.content {
display: none;
}
<button type=\"button\" class=\"button\" style=\"position: static; left: 100;\">For Copper Rollers</button>
<div class=\"content\" style=\" width: 48%; background-color: lightblue; padding: 10px; border-radius: 10px; margin-right: 5px; \">
</div>
<button class=\"button\" type=\"button\" style=\"position: static; left: 175px; \">For Rubber Rollers</button>
<div class=\"content\" style=\"margin-left:50%; float: left; width: 48%; background-color: lightblue; padding: 10px; border-radius: 10px;\">
</div>
-
contentdiv 是否都出现在相同的水平偏移量(即:left:50% 原样)或相对于它们的父按钮? -
在相同的水平偏移上。
-
这是否意味着所有内容 div 都将在彼此下方对齐到页面的一侧作为第一个?
-
第一个内容 div 是页面的前半部分,下一个在第二半部分与之相邻。 (彼此相邻)
标签: javascript html css position