I would like to find a more efficienct way to use jQuery for my question and answer page.
Here is the code which I want to change. If a .q_container
is clicked, I want its corresponding answer div
will slide down.
$(document).ready(function() {
$('#qc1').hover(
function () {
$('#a1').slideDown('fast');
},
function () {
$('#a1').slideUp('fast');
}
);
$("#qc2").hover(
function () {
$('#a2').slideDown('fast');
},
function () {
$('#a2').slideUp('fast');
}
);
$("#qc3").hover(
function () {
$('#a3').slideDown('fast');
},
function () {
$('#a3').slideUp('fast');
}
);
});
All of my Code:
<html>
<head>
<style type="text/css">
* {
padding: 0px;
margin: 0px;
}
body {
background-color: black;
}
#container {
width:1000px;
min-height: 500px;
background-color: #3b3b3b;
padding: 10px;
margin-left: auto;
margin-right: auto;
}
.q_container {
width:300px;
}
.question {
border-radius: 5px 5px 0px 0px;
width:300px;
height:30px;
background-color: red;
padding: 4px;
}
.answer {
border-radius: 0px 0px 5px 5px;
width:300px;
height:100px;
display:none;
background-color: blue;
overflow: hidden;
padding: 4px;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
$('#qc1').hover(
function () {
$('#a1').slideDown('fast');
},
function () {
$('#a1').slideUp('fast');
}
);
$("#qc2").hover(
function () {
$('#a2').slideDown('fast');
},
function () {
$('#a2').slideUp('fast');
}
);
$("#qc3").hover(
function () {
$('#a3').slideDown('fast');
},
function () {
$('#a3').slideUp('fast');
}
);
});
</script>
</head>
<body>
<div id="container">
<div class="q_container" id="qc1">
<div class="question">
What is a question?
</div>
<div class="answer" id="a1">
It is a way to discover something.
</div>
</div>
<div style="width:300px;height:10px;"></div>
<div class="q_container" id="qc2">
<div class="question">
What is a question2?
</div>
<div class="answer" id="a2">
It is a way to discover something2.
</div>
</div>
<div style="width:300px;height:10px;"></div>
<div class="q_container" id="qc3">
<div class="question">
What is a question3?
</div>
<div class="answer" id="a3">
It is a way to discover something3.
</div>
</div>
</div>
</body>
</head>