ホームページ  >  記事  >  ウェブフロントエンド  >  jquery+cssによるドロップダウンリスト機能の実装例を詳しく解説

jquery+cssによるドロップダウンリスト機能の実装例を詳しく解説

小云云
小云云オリジナル
2017-12-29 11:18:291022ブラウズ

この記事では、jquery と css を組み合わせて実装されたドロップダウン リスト関数をサンプル コードを通して紹介します。必要な方は参考にしていただければ幸いです。

これ以上ナンセンスではありません。具体的なコードは次のとおりです:


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>fruit</title>
<style type="text/css">
.hide {
 display: none;
}
p {
 float: left;
 width: 100%;
}
.selector-containter {
 margin-bottom: 10px;
}
.selector {
 width: 200px;
 background: #FFF;
 border: 1px solid #DDD;
}
.selector-hint {
 width: 178px;
 border: 1px solid #DDD;
}
.selector-expand {
 width: 8px;
 border: 1px solid #DDD;
}
.selector-collapse {
 width: 8px;
 border: 1px solid #DDD;
}
</style>
<script src="jquery-3.2.1.min.js"></script>
<script>
$(document).ready(function() {
  //使用on方法,采用事件委派机制,selector-option-container中的内容为后续动态追加
  $(&#39;.selector&#39;).on(&#39;click&#39;, &#39;.selector-expand&#39;, function() {
    $(this).parent().children(&#39;.selector-option-container&#39;).children().remove();
    $(this).parent().children(&#39;.selector-option-container&#39;).append(&#39;<p><input type="checkbox" name="fruitGroup" class="selector-checkbox"/></p><p class="selector-option">apricot</p>&#39;);
    $(this).parent().children(&#39;.selector-option-container&#39;).append(&#39;<p><input type="checkbox" name="fruitGroup" class="selector-checkbox"/></p><p class="selector-option">banana</p>&#39;);
    $(this).nextAll(&#39;.selector-option-container&#39;).removeClass(&#39;hide&#39;);
  });
  $(&#39;.selector&#39;).on(&#39;click&#39;, &#39;.selector-collapse&#39;, function() {
    $(this).nextAll(&#39;.selector-option-container&#39;).addClass(&#39;hide&#39;);
  });
  $(&#39;.selector-t1&#39;).on(&#39;click&#39;, &#39;.selector-option&#39;, function() {
    $(this).parent().parent().children(&#39;.selector-hint&#39;).text($(this).text());
    $(this).parent().addClass(&#39;hide&#39;);
  });
  $(&#39;.selector-t1&#39;).on(&#39;click&#39;, &#39;.selector-checkbox&#39;, function() {
    $(this).parent().parent().parent().children(&#39;.selector-hint&#39;).text($(this).parent().next().text());
    //采用prop方法,对于值为布尔型的属性赋值
    $(this).prop(&#39;checked&#39;, false);
    $(this).parent().parent().addClass(&#39;hide&#39;);
  });
});
</script>
</head>
<body>
<p id="titan" class="selector-containter">
<p id="fruit">
 <p class="selector">
  <p class="selector-hint">select fruit</p>
  <p class="selector-expand">+</p>
  <p class="selector-collapse">-</p>
  <p class="selector-option-container">
  </p>
 </p>
</p>
</p>
<p id="athena" class="selector-t1 selector-containter">
<p id="fruit">
 <p class="selector">
  <p class="selector-hint">select fruit</p>
  <p class="selector-expand">+</p>
  <p class="selector-collapse">-</p>
  <p class="selector-option-container">
  </p>
 </p>
</p>
</p>
</body>
</html>

関連する推奨事項:

WeChat アプレットの表示ドロップダウン リスト機能を実装する方法

Vueについて .jsフォームタグのラジオボタン、チェックボタン、ドロップダウンリストの値の詳しい説明

ドロップダウンリストボックスのネイティブjs実装

以上がjquery+cssによるドロップダウンリスト機能の実装例を詳しく解説の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。