jQueryを使用してトグルボタンを作成するにはどうすればよいですか? Wordや他のテキストエディタから "B"、 "I"、 "U"のようなボタンが必要です。JQueryを使用してトグルボタンを作成する方法は?
-4
A
答えて
2
this JSFiddle codeがあなたに役立つかどうかを確認してください。
<p><b>Disclaimer:</b> Due to a webkit bug involving pseudo classes and sibling selectors this may not work in some webkit-based browsers.</p>
<p>Here's our finished toggle button.</p>
<label id="label">
<input type="checkbox" id="checkbox" />
<span id="off">Off</span>
<span id="on">On</span>
</label>
<p>Here's how we build it. First you start off with a label containing a checkbox and two spans.</p>
<label id="label">
<input type="checkbox" />
<span>Off</span>
<span>On</span>
</label>
<p>Simple and not much to look at. Let's style those two spans to look like buttons and hide the checkbox.</p>
<label id="label">
<input type="checkbox" id="checkbox" />
<span id="off">Off</span>
<span id="on" style="display:block;">On</span>
</label>
<p>Now, the key is to hide the "On" span initially with "display: none;". Then we show it when the checkbox is checked while hiding the "Off" span at the same time. This is done with this CSS:</p>
<pre>
#checkbox:checked + #off {
display: none;
}
#checkbox:checked + #off + #on {
display: block;
}
</pre>
<p>So, when the checkbox is checked we are targeting its sibling "off" and we are also targeting the sibling of the checkbox's sibling which is "on".</p>
<p>At this point you could use server-side code or javascript to react to the hidden checkbox being checked just like you would with a normal checkbox.</p>
p {
margin: 10px;
}
pre {
margin: 10px;
}
#label {
cursor: pointer;
display: block;
margin: 20px auto;
width: 200px;
}
#checkbox {
display: none;
}
#checkbox:checked + #off {
display: none;
}
#checkbox:checked + #off + #on {
display: block;
}
#off {
background: #ff3019;
background: -moz-linear-gradient(top, #ff3019 0%, #cf0404 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ff3019), color-stop(100%,#cf0404));
background: -webkit-linear-gradient(top, #ff3019 0%,#cf0404 100%);
background: -o-linear-gradient(top, #ff3019 0%,#cf0404 100%);
background: -ms-linear-gradient(top, #ff3019 0%,#cf0404 100%);
background: linear-gradient(top, #ff3019 0%,#cf0404 100%);
border: 2px solid #cf0404;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
-o-border-radius: 10px;
border-radius: 10px;
color: white;
display: block;
font: 1em "Amaranth", sans-serif;
padding: 4px 10px;
text-align: center;
-moz-user-select: none;
-webkit-user-select: none;
-o-user-select: none;
user-select: none;
}
#on {
background: #b4ddb4;
background: -moz-linear-gradient(top, #b4ddb4 0%, #83c783 17%, #52b152 33%, #008a00 67%, #005700 83%, #002400 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#b4ddb4), color-stop(17%,#83c783), color-stop(33%,#52b152), color-stop(67%,#008a00), color-stop(83%,#005700), color-stop(100%,#002400));
background: -webkit-linear-gradient(top, #b4ddb4 0%,#83c783 17%,#52b152 33%,#008a00 67%,#005700 83%,#002400 100%);
background: -o-linear-gradient(top, #b4ddb4 0%,#83c783 17%,#52b152 33%,#008a00 67%,#005700 83%,#002400 100%);
background: -ms-linear-gradient(top, #b4ddb4 0%,#83c783 17%,#52b152 33%,#008a00 67%,#005700 83%,#002400 100%);
background: linear-gradient(top, #b4ddb4 0%,#83c783 17%,#52b152 33%,#008a00 67%,#005700 83%,#002400 100%);
border: 2px solid #002400;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
-o-border-radius: 10px;
border-radius: 10px;
color: white;
display: none;
font: 1em "Amaranth", sans-serif;
padding: 4px 10px;
text-align: center;
-moz-user-select: none;
-webkit-user-select: none;
-o-user-select: none;
user-select: none;
}
1
jQuery UIには、チェックボックス用にレンダリングされたボタンが用意されています。このソリューションを参照してください:http://jqueryui.com/demos/button/#checkbox
関連する問題
- 1. .. jqueryとvb.netを使用してトグルボタンを作成する方法は?
- 2. Bootstrap Toggleプラグインを使用してトグルボタンを作成する方法は?
- 3. jquery mobileを使用してタブを作成する方法は?
- 4. JQueryを使用してLinkButtonを作成する方法
- 5. jqueryを使用して全幅ドロップダウンメニューを作成する方法
- 6. AJAXオプションを使用してJQueryデータテーブルを作成する方法
- 7. jQuery Mobileを使用してTelerikMVC3を作成する方法
- 8. jqueryを使用してasp.netでカスタムコントロールを作成する方法
- 9. jQueryを使用してiframeを作成し、ページに表示する方法は?
- 10. jqueryを使用してボタンとデータピッカーを作成する方法は?
- 11. jQueryを使用してJSONオブジェクトを配列に作成する方法は?
- 12. CSS3、jQuery、およびHTMLを使用してイメージキューエフェクトを作成する方法は?
- 13. jQueryを使用してHTML入力を動的に作成する方法は?
- 14. Jqueryを使用してASP.Netでダイアログボックスを作成する方法は?
- 15. jqueryを使用して画像のズーム効果を作成する方法は?
- 16. JavaScriptのトグルボタンを作成するには?
- 17. アンドロイド:カスタムトグルボタンを作成するにはトグルボタン
- 18. アンドロイドのトグルボタンを使用してシリアライザブルを実装する方法は?
- 19. FullCalendarで 'editable'プロパティのトグルボタンを作成する方法
- 20. jqueryなしでtitle属性を使用してカスタムツールチップを作成する方法
- 21. PHP、JSON、JQueryを使用して動的ページを作成する方法
- 22. jqueryを使用してテーブルの対角を作成する方法
- 23. "サブ関数"のオプションを使用してjquery関数を作成する方法
- 24. HTML、CSS、およびJQueryを使用してCNNメニューナビゲーションを作成する方法
- 25. jquery/javascriptを使用して複数のフィルタリストを作成する方法
- 26. jqueryプラグインを使用して複数のイベントを作成する方法
- 27. MVC3 aspxビューエンジンでjqueryを使用してカレンダーコントロールを作成する方法
- 28. jqueryを使用してhtmlテーブルにドロップダウンを作成する方法
- 29. Jqueryを使用して簡単なHTMLプレイリストを作成する方法
- 30. jquery/javascriptにループを使用してメソッドを作成する方法
すでに試したことをいくつか示してください。 Javascript/jQueryの質問にJFiddleを含めることは常に役立ちます。 –