Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h2 class="my-5">jQueryのアコーディオン</h2>
<ul id="menu">
  <li class="menu-item">
    <h3 class="menu-item-link">メニュー1(クリックで開く)</h3>
    <span>+</span>
    <ul class="sub-menu">
      <li class="sub-menu-item"><a href="">サブメニュー1-1</a></li>
      <li class="sub-menu-item"><a href="">サブメニュー1-2</a></li>
      <li class="sub-menu-item"><a href="">サブメニュー1-3</a></li>
      <li class="sub-menu-item"><a href="">サブメニュー1-4</a></li>
    </ul>
  </li>
  <li class="menu-item">
    <h3>メニュー2(クリックで開く)</h3>
    <span>+</span>
    <ul class="sub-menu">
      <li class="sub-menu-item"><a href="">サブメニュー2-1</a></li>
      <li class="sub-menu-item"><a href="">サブメニュー2-2</a></li>
      <li class="sub-menu-item"><a href="">サブメニュー2-3</a></li>
      <li class="sub-menu-item"><a href="">サブメニュー2-4</a></li>
    </ul>
  </li>
  <li class="menu-item">
    <h3>メニュー3(クリックで開く)</h3>
    <span>+</span>
    <ul class="sub-menu">
      <li class="sub-menu-item"><a href="">サブメニュー3-1</a></li>
      <li class="sub-menu-item"><a href="">サブメニュー3-2</a></li>
      <li class="sub-menu-item"><a href="">サブメニュー3-3</a></li>
      <li class="sub-menu-item"><a href="">サブメニュー3-4</a></li>
    </ul>
  </li>
  <li class="menu-item">
    <h3>メニュー4(クリックで開く)</h3>
    <span>+</span>
    <ul class="sub-menu">
      <li class="sub-menu-item"><a href="">サブメニュー4-1</a></li>
      <li class="sub-menu-item"><a href="">サブメニュー4-2</a></li>
      <li class="sub-menu-item"><a href="">サブメニュー4-3</a></li>
      <li class="sub-menu-item"><a href="">サブメニュー4-4</a></li>
    </ul>
  </li>
</ul>
              
            
!

CSS

              
                .menu-item{
	position: relative;
	border-bottom:1px solid #ccc;
	margin-bottom: 20px;
}
.menu-item h3{
	font-size: 16px;
	cursor:pointer;
}
.menu-item span{
	position: absolute;
	top: 0;
	right: 5px;
	color: #ccc
}
.sub-menu{
	display: none;
	padding: 5px 0px;
	margin-bottom: 15px;
}
              
            
!

JS

              
                $(function(){
	// 親メニューがクリックされたら
	$('.menu-item').click(function(){
		// クリックされた親メニューの子要素(サブメニュー)を取得
		var $subMenu = $(this).find('.sub-menu');
		// サブメニューに「.open」があれば
		if($subMenu.hasClass('open')){
			// サブメニューから「.open」を外して、非表示にする
			$subMenu.removeClass('open');
			$subMenu.slideUp();
			$(this).find('span').text('+');
		// サブメニューに「.open」がなければ
		}else{
			// サブメニューに「.open」をつけて、表示する
			$subMenu.addClass('open');
			$subMenu.slideDown();
			$(this).find('span').text('-');
		}
	});
});
              
            
!
999px

Console