主要div中有四个主要div我有一个按钮和另一个容器div。当我按下按钮时,容器div是可见的,按钮的文字会改变,但所有的主要div都可见我的问题是我想要隐藏所有其他div,按下按钮的主div应该与容器div休息一起可见其他主要的div应该是隐藏的。但我无法隐藏其他主要的divs
<!DOCTYPE html>
<html>
<head>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'></script>
<script>
$(document).ready(function(){
$('.myButton').click(function(){
if ( this.value == 'collapse' ) {
// if it's open close it
open = false;
this.value = 'expand';
$(this).next("div.container").hide("slow");
}
else {
// if it's close open it
open = true;
this.value = 'collapse';
$(this).next("div.container").show("slow");
}
});
});
</script>
</head>
<body>
<?php
for($i=0;$i<=3;$i++)
{
echo"<div class='main' style='position:relative;border:1px solid #A5BEBE;background-color: #E7EFEF;width:84%; '>
<b> sometext:-</b><a href='#'>$url</a><br>
<b>sometext :</b>$b<br>
<b>sometext :</b>$c<br>
<b>sometext :</b>$d<br>
<b>sometext :</b>$e<br>
<b>sometext :</b>$f<br>
<b>sometext :</b>$g</p>
<input type='button' value='expand' class='myButton' target='".$i."' style='position:absolute;left:85%;top:4%;background:#B20000;color:white;width:70px;height:20px;font- size:15px;' >$i";
echo "<div class='container' style='display:none;background:white; '>
<b> sometext:-</b><a href='#'>$url</a><br>
<b>sometext :</b>$b<br>
<b>sometext :</b>$c<br>
<b>sometext :</b>$d<br>
<b>sometext :</b>$e<br>
<b>sometext :</b>$f<br>
<b>sometext :</b>$g</p>
</div>";
echo "</div><br><br>";
}
?>
</body>
</html>
当我们点击一个按钮时,会举行两个事件 1: - 隐藏所有其他主div和容器div,留下按下按钮的一个div 2: - 按下按钮的div,显示主div和容器div,按钮文本更改为折叠
当我们再次点击按钮时,举行的活动是 1: - 显示所有主要div并隐藏容器div 2: - 按钮的文本更改为展开 在上述事件中,已经取得了许多成就。问题是我们无法隐藏主要div的其余部分,而当前主要div已按下按钮
答案 0 :(得分:2)
您可以隐藏其他容器,例如
$(document).ready(function() {
var $mains = $('.main');
var $bts = $('.myButton').click(function() {
var $ct = $(this).next("div.container"),
$main = $(this).closest('.main');
if (this.value == 'collapse') {
this.value = 'expand';
$ct.hide("slow");
$mains.not($main).show()
} else {
this.value = 'collapse';
$ct.show("slow");
$mains.not($main).hide()
}
//hide other container elements
$('div.container').not($ct).hide();
$bts.not(this).val('expand');
});
});
$(document).ready(function() {
var $mains = $('.main');
var $bts = $('.myButton').click(function() {
var $ct = $(this).next("div.container"),
$main = $(this).closest('.main');
if (this.value == 'collapse') {
this.value = 'expand';
$ct.hide("slow");
$mains.not($main).show()
} else {
this.value = 'collapse';
$ct.show("slow");
$mains.not($main).hide()
}
//hide other container elements
$('div.container').not($ct).hide();
$bts.not(this).val('expand');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class='main' style='position:relative;border:1px solid #A5BEBE;background-color: #E7EFEF;width:84%; '>
<b> sometext:-</b><a href='#'>$url</a>
<br>
<b>sometext :</b>$b
<br>
<b>sometext :</b>$c
<br>
<b>sometext :</b>$d
<br>
<b>sometext :</b>$e
<br>
<b>sometext :</b>$f
<br>
<b>sometext :</b>$g
<input type='button' value='expand' class='myButton' target='".$i."' style='position:absolute;left:85%;top:4%;background:#B20000;color:white;width:70px;height:20px;font- size:15px;'>$i"; echo "
<div class='container' style='display:none;background:white; '> <b> sometext:-</b><a href='#'>$url</a>
<br>
<b>sometext :</b>$b
<br>
<b>sometext :</b>$c
<br>
<b>sometext :</b>$d
<br>
<b>sometext :</b>$e
<br>
<b>sometext :</b>$f
<br>
<b>sometext :</b>$g</div>
</div>
<div class='main' style='position:relative;border:1px solid #A5BEBE;background-color: #E7EFEF;width:84%; '>
<b> sometext:-</b><a href='#'>$url</a>
<br>
<b>sometext :</b>$b
<br>
<b>sometext :</b>$c
<br>
<b>sometext :</b>$d
<br>
<b>sometext :</b>$e
<br>
<b>sometext :</b>$f
<br>
<b>sometext :</b>$g
<input type='button' value='expand' class='myButton' target='".$i."' style='position:absolute;left:85%;top:4%;background:#B20000;color:white;width:70px;height:20px;font- size:15px;'>$i"; echo "
<div class='container' style='display:none;background:white; '> <b> sometext:-</b><a href='#'>$url</a>
<br>
<b>sometext :</b>$b
<br>
<b>sometext :</b>$c
<br>
<b>sometext :</b>$d
<br>
<b>sometext :</b>$e
<br>
<b>sometext :</b>$f
<br>
<b>sometext :</b>$g</div>
</div>
答案 1 :(得分:1)
$(document).ready(function(){
$('.myButton').click(function() {
var $ct = $(this).next("div.container");
if (this.value == 'collapse') {
this.value = 'expand';
$ct.hide("slow");
$('.myButton').parent().show("slow");
} else {
this.value = 'collapse';
$ct.show("slow");
$('.myButton').not($(this)).parent().hide("hide");
}
});
});
这将隐藏所有其他div
$(document).ready(function() {
$('.myButton').click(function() {
var $ct = $(this).next("div.container");
if (this.value == 'collapse') {
this.value = 'expand';
$ct.hide("slow");
$('.myButton').parent().show("slow");
} else {
this.value = 'collapse';
$ct.show("slow");
$('.myButton').not($(this)).parent().hide("hide");
}
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class='main' style='position:relative;border:1px solid #A5BEBE;background-color: #E7EFEF;width:84%; '>
<b> sometext:-</b><a href='#'>$url</a>
<br>
<b>sometext :</b>$b
<br>
<b>sometext :</b>$c
<br>
<b>sometext :</b>$d
<br>
<b>sometext :</b>$e
<br>
<b>sometext :</b>$f
<br>
<b>sometext :</b>$g
<input type='button' value='expand' class='myButton' target='".$i."' style='position:absolute;left:85%;top:4%;background:#B20000;color:white;width:70px;height:20px;font- size:15px;'>$i"; echo "
<div class='container' style='display:none;background:white; '> <b> sometext:-</b><a href='#'>$url</a>
<br>
<b>sometext :</b>$b
<br>
<b>sometext :</b>$c
<br>
<b>sometext :</b>$d
<br>
<b>sometext :</b>$e
<br>
<b>sometext :</b>$f
<br>
<b>sometext :</b>$g</div>
</div>
<div class='main' style='position:relative;border:1px solid #A5BEBE;background-color: #E7EFEF;width:84%; '>
<b> sometext:-</b><a href='#'>$url</a>
<br>
<b>sometext :</b>$b
<br>
<b>sometext :</b>$c
<br>
<b>sometext :</b>$d
<br>
<b>sometext :</b>$e
<br>
<b>sometext :</b>$f
<br>
<b>sometext :</b>$g
<input type='button' value='expand' class='myButton' target='".$i."' style='position:absolute;left:85%;top:4%;background:#B20000;color:white;width:70px;height:20px;font- size:15px;'>$i"; echo "
<div class='container' style='display:none;background:white; '> <b> sometext:-</b><a href='#'>$url</a>
<br>
<b>sometext :</b>$b
<br>
<b>sometext :</b>$c
<br>
<b>sometext :</b>$d
<br>
<b>sometext :</b>$e
<br>
<b>sometext :</b>$f
<br>
<b>sometext :</b>$g</div>
</div>
&#13;