可折疊面板
舉個例子
<html>
<div id="accord">
<h3>Part1</h3>
<p> I am part 1</p>
<h3>Part2</h3>
<p>I am part 2</p>
<h3>part 3</h3>
<p>I am part 3</p>
</div>
<script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$("#accord").on('click','h3',function(e){
//綁定單擊事件
$(this).next().slideToggle();
//深入理解next選擇器
})
</script>
</html>
分析
- 關(guān)鍵在于理解next方法
- 以及slideToggle方法
- this指代的是當(dāng)前元素h3
- next選擇緊鄰的同級別元素搜吧,不是說標簽相同畜疾,比如這個例子p和h3就是同級別
- 上述是一個鏈式操作
其中
$(this).next().slideToggle();
也可以寫成
$(this)
.next()
.slideToggle();