我有一个系统可以切换不同的部分。它几乎是我想要的,除了某些原因,当我点击不同的块时,div project-name-block-container
中的所有内容都没有显示。图像显示在第一个区块中,但那就是它。自从我用div包装所有内容后,不应该显示所有内容吗?
还有一种方法可以保持我所拥有的功能,除了改变一件事。而不是能够点击project-name-block
切换块,有没有办法让用户必须点击标题?
var allPanels = $('.project-name-block-container').hide();
//$('.project-name-block:eq(0) .project-name-description').show();
$('.project-name-block').click(function() {
allPanels.slideUp();
$(this).children('.project-name-block-container').slideDown();
return false;
});

#project-section-container {
height: auto;
}
.project-name-block {
border-top: 1px solid black;
width: 100%;
cursor: pointer;
padding: 50px 0;
text-align: center;
}
.project-name-title {
color: blue;
font-size: 2em;
}
.project-name-description {
display: none;
font-size: 1em;
margin-top: 25px;
}
/*.project-name-description.active {
display: block;
}*/
/*--Project Images---*/
.project-image-container {
width: 90%;
margin: 50px 5%;
}
.project-img {
width: 30%;
height: auto;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="project-name-block">
<div class="project-name-title">Project 1</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 1</div>
<div class="project-image-container">
<img src="images/projects/project1.jpg" alt="Demolition Project" class="project-img">
<img src="images/projects/project2.jpg" alt="Demolition Project" class="project-img">
</div>
</div>
</div>
<div class="project-name-block">
<div class="project-name-title">Project 2</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 2</div>
</div>
</div>
<div class="project-name-block">
<div class="project-name-title">Project 3</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 3</div>
</div>
</div>
<div class="project-name-block">
<div class="project-name-title">Project 4</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 4</div>
</div>
</div>
<div class="project-name-block">
<div class="project-name-title">Project 5</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 5</div>
</div>
</div>
</div>
&#13;
答案 0 :(得分:2)
您有display:none
的说明...删除
.project-name-description {
/*display: none;*/ REMOVE THIS LINE
font-size: 1em;
margin-top: 25px;
}
如果您想要项目的标题触发该功能,只需更改该选择器而不是children
使用next
:
var allPanels = $('.project-name-block-container');
$('.project-name-title').click(function() {
allPanels.slideUp();
$(this).next('.project-name-block-container').slideDown();
return false;
});
&#13;
#project-section-container {
height: auto;
}
.project-name-block {
border-top: 1px solid black;
width: 100%;
cursor: pointer;
padding: 50px 0;
text-align: center;
}
.project-name-title {
color: blue;
font-size: 2em;
}
.project-name-description {
font-size: 1em;
margin-top: 25px;
}
.project-image-container {
width: 90%;
margin: 50px 5%;
}
.project-img {
width: 30%;
height: auto;
}
.project-name-block-container {
display: none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="project-name-block">
<div class="project-name-title">Project 1</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 1</div>
<div class="project-image-container">
<img src="images/projects/project1.jpg" alt="Demolition Project" class="project-img">
<img src="images/projects/project2.jpg" alt="Demolition Project" class="project-img">
</div>
</div>
</div>
<div class="project-name-block">
<div class="project-name-title">Project 2</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 2</div>
</div>
</div>
<div class="project-name-block">
<div class="project-name-title">Project 3</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 3</div>
</div>
</div>
<div class="project-name-block">
<div class="project-name-title">Project 4</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 4</div>
</div>
</div>
<div class="project-name-block">
<div class="project-name-title">Project 5</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 5</div>
</div>
</div>
</div>
&#13;
其他强>
作为对实际代码的改进,如果你想&#34;切换&#34;元素检查此代码段:
$('.project-name-title').click(function() {
var relative = $(this).next('.project-name-block-container');
if (!relative.hasClass('opened')) {
$('.opened').slideUp().removeClass('opened');
relative.addClass('opened').slideDown();
} else {
relative.slideUp().removeClass('opened');
}
return false;
});
&#13;
#project-section-container {
height: auto;
}
.project-name-block {
border-top: 1px solid black;
width: 100%;
cursor: pointer;
padding: 50px 0;
text-align: center;
}
.project-name-title {
color: blue;
font-size: 2em;
}
.project-name-description {
font-size: 1em;
margin-top: 25px;
}
.project-image-container {
width: 90%;
margin: 50px 5%;
}
.project-img {
width: 30%;
height: auto;
}
.project-name-block-container {
display: none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="project-name-block">
<div class="project-name-title">Project 1</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 1</div>
<div class="project-image-container">
<img src="images/projects/project1.jpg" alt="Demolition Project" class="project-img">
<img src="images/projects/project2.jpg" alt="Demolition Project" class="project-img">
</div>
</div>
</div>
<div class="project-name-block">
<div class="project-name-title">Project 2</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 2</div>
</div>
</div>
<div class="project-name-block">
<div class="project-name-title">Project 3</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 3</div>
</div>
</div>
<div class="project-name-block">
<div class="project-name-title">Project 4</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 4</div>
</div>
</div>
<div class="project-name-block">
<div class="project-name-title">Project 5</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 5</div>
</div>
</div>
</div>
&#13;
答案 1 :(得分:1)
这应该可以解决问题。你在display:none
div上有project-name-description
,没有显示任何内容。
var allPanels = $('.project-name-block-container').hide();
//$('.project-name-block:eq(0) .project-name-description').show();
$('.project-name-title').click(function() {
allPanels.slideUp();
$(this).parent().find('.project-name-block-container').slideDown();
return false;
});
.project-name-description{font-size:3rem;}
#project-section-container {
height: auto;
}
.project-name-block {
border-top: 1px solid black;
width: 100%;
cursor: pointer;
padding: 50px 0;
text-align: center;
}
.project-name-title {
color: blue;
font-size: 2em;
}
.project-name-description {
XXdisplay: none;
font-size: 1em;
margin-top: 25px;
}
/*.project-name-description.active {
display: block;
}*/
/*--Project Images---*/
.project-image-container {
width: 90%;
margin: 50px 5%;
}
.project-img {
width: 30%;
height: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="project-name-block">
<div class="project-name-title">Project 1</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 1</div>
<div class="project-image-container">
<img src="images/projects/project1.jpg" alt="Demolition Project" class="project-img">
<img src="images/projects/project2.jpg" alt="Demolition Project" class="project-img">
</div>
</div>
</div>
<div class="project-name-block">
<div class="project-name-title">Project 2</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 2</div>
</div>
</div>
<div class="project-name-block">
<div class="project-name-title">Project 3</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 3</div>
</div>
</div>
<div class="project-name-block">
<div class="project-name-title">Project 4</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 4</div>
</div>
</div>
<div class="project-name-block">
<div class="project-name-title">Project 5</div>
<div class="project-name-block-container">
<div class="project-name-description">This is the text for Project 5</div>
</div>
</div>