2012-06-14 96 views
0
<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
<title>FAQ Section</title> 
<link rel="stylesheet" type="text/css" href="styles.css"/> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 

<script type="text/javascript"> 
$(document).ready(function(){ 
    $("h2").click(function(){ 
    $("p").toggle("fast"); 
    }); 
}); </script> 

</head> 

<body> 
    <h1>FAQ</h1> 

    <div> 
    <!-- The FAQs are inserted here --> 

     <h2>Question1?</h2> 
     <p>Answer...</p> 
     <h2>Question2?</h2> 
     <p>Answer...</p> 
     <h2>Question3?</h2> 
     <p>Answer...</p> 
    </div> 

</body> 

</html> 

这是我的示例页面:http://www.kursatkarabulut.com/kopya.htmljquery切换如何使用个人元素没有个人ID?

我试图做的是对我个人网站的常见问题页面。将会有大约40个问题。我是jQuery的新手。我用每个问题的切换。只有问题会在开始时显示,当用户点击一个问题时,它会显示在下面,再次点击它会隐藏。现在,当我点击一个问题时,它会展开并收缩所有问题。

有没有办法单独做到这一点,而不使用每个标题的个人ID? 以及如何加载页面切换关闭?

谢谢。

+0

你可以做到这一点,但它会更容易重组代码位 – Huangism

回答

2

试试这个...

$("h2").click(function(){ 
    $(this).next().toggle("fast"); 
    }); 

,这将会使页面加载切换:

$("h2").click(); 

或者,如果你不想在页面加载的切换动画,你可以这样做:

$("p").toggle(); 

See here for a working example

+0

非常感谢的答案,这让我很快乐。 – showbiz

1

尝试:

$("h2").click(function(){ 
    $(this).next().toggle("fast"); 
}); 
+0

例如:http://jsfiddle.net/PqLej/2/ –

0

试试这个

$(document).ready(function(){ 
    $("h2").click(function(){ 
    $(this).next("p").toggle("fast"); 
    }); 
});