我已经编写了一个代码,我有一个框,当我点击按钮时,它实际上显示了或多或少的信息。在此处查看此操作:https://jsfiddle.net/075tcezL/
/css
#model {
width: 300px;
border: 1px solid #c1c1c1;
margin: 20px 10px;
padding: 0 5px;
float: left;
max-height: 300px;
overflow: hidden;
-webkit-transition: max-height 0.7s;
-moz-transition: max-height 0.7s;
transition: max-heigth 0.7s;
}
#show-more {
display: block;
background-color: #75868E;
width: 100px;
font-size: 12px;
text-transform: uppercase;
display: inline-block;
margin: 20px auto;
cursor: pointer;
height: 15px;
padding: 10px 0;
}
#model.open {
max-height: 1000px;
-webkit-transition: max-heigth 0.7s;
-moz-transition: max-heigth 0.7s;
transition: max-heigth 0.7s;
}
// HTML
<body>
<div id="model" class="model1">
<h3>Series 3</h3>
<a href="#series3" id="sseries"><img class="image"
src="image"></a>
<p>Text to hide or show.</p>
</div>
<a id="show-more" class="show">Show More</a>
<script src="javascript.js"></script>
</body>
//这适用于一个方框
var content = document.getElementById("model");
var button = document.getElementById("show-more")
button.onclick = function(){
if(content.className == "open"){
content.className = "";
button.innerHTML = "More";
} else {
content.className = "open";
button.innerHTML = "Less";
}
};
但如果我有三个盒子,就像这样 - https://jsfiddle.net/jw7pfb1w/ 如何在单击该按钮的同时打开或关闭它们?
任何帮助将不胜感激!
答案 0 :(得分:0)
您可以使用相同的方法三次,也可以循环使用。
为了达到这个目的,你可以在你的html中放置多个带数字的ID。 像model1,model2,model3,...
var button = document.getElementById("show-more")
button.onclick = function(){
// we loop three times for three blocks
for (var i = 0; i < 3; i++) {
// i + 1 -> 1, 2, 3
// so model1, then model2, then model3
// using concatenation string "model" + the number
var content = document.getElementById("model" + (i + 1));
// using toggle function will add the class if not present, else remove it.
var isOpen = content.classList.toggle('open');
button.innerHTML = isOpen ? "More" : "Less";
}
};
.model {
width: 100px;
border: 1px solid #c1c1c1;
margin: 20px 10px;
padding: 0 5px;
display: inline-block;
max-height: 100px;
overflow: hidden;
}
.open {
max-height: 1000px !important;
}
<button id="show-more">More</button>
<div>
<div class="model" id="model1">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
<div class="model" id="model2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
<div class="model" id="model3">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
</div>
答案 1 :(得分:-1)
您可以使用document.getElementsByClassName('.model1');
选择页面上具有相同类别的所有元素:
var content = document.getElementsByClassName("model1");
var button = document.getElementById("show-more");
button.onclick = function(){
for (i = 0; i < content.length; ++i) {
if(content[i].className == "model1 open"){
content[i].className = "model1";
button.innerHTML = "More";
} else {
content[i].className = "model1 open";
button.innerHTML = "Less";
}
}
};
在3面板示例中,您还需要open
的CSS:
div.open {
max-height: 1000px;
-webkit-transition: max-heigth 0.7s;
-moz-transition: max-heigth 0.7s;
transition: max-heigth 0.7s;
}
为您添加了一个工作小提琴:https://jsfiddle.net/jw7pfb1w/5/