![](/img/trans.png)
[英]How to assign a css class to a radio-button group in a radio button list?
[英]How to use the method : $().button('toggle') for this bootstrap 'radio-button list'?
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-secondary active">
<input type="radio" name="options" id="option1" autocomplete="off"
checked> Active
</label>
<label class="btn btn-secondary">
<input type="radio" name="options" id="option2" autocomplete="off">
Radio
</label>
<label class="btn btn-secondary">
<input type="radio" name="options" id="option3" autocomplete="off">
Radio
</label>
</div>
$().button('toggle') :切換推送狀態。 使按鈕具有已激活的外觀。
RadioButtonList 圖像的屏幕截圖。 從最左邊的按鈕可以看到,當它被點擊時,它看起來像是被點擊了。
我目前正在制作的登錄頁面的屏幕截圖。 當我點擊其中一個“單選”按鈕時,它們的外觀沒有改變,所以我必須使用 $().button('toggle') 方法,但我不知道在哪里或如何使用它。
您只需要向jQuery函數添加正確的選擇器即可。 嘗試使用此代替:
$(".btn-group").button("toggle");
現在您的代碼應該可以工作了。
希望這會有所幫助!
我遇到這個問題是因為我被一個類似的用例難住了。 這是我第一次使用 jquery,所以起初我對語法有點困惑。 (tl;博士 JS/jquery 之間的差異見 4。)
我想擴展第一個答案,因為我會感謝其他人做同樣的事情。 我在 Bootstrap 4.5.3 上解決了這個問題幾個小時,完全不明白為什么我的單選按鈕不會顯示切換效果,然后我意識到button()
方法有一個庫要求,我做到了在文檔中看不到。
buttons.js
庫位於node_modules/bootstrap/js/dist/
加載 + 初始化示例:
<script src="node_modules/bootstrap/js/dist/buttons.js">
$('.btn-group').button('toggle');
</script>
您可以分解 jquery 並加載腳本,如果這有助於您保持簡潔(每個單獨的<script />
標簽):
<script src="node_modules/bootstrap/js/dist/buttons.js"> </script>
<script>$('.btn-group').button('toggle');</script>
$('[data-toggle="button"]').button('toggle');
關於通過數據屬性選擇元素的更多信息: 使用 jQuery 通過數據屬性選擇元素
#
哈希符號表示 ID 選擇器( .
句點符號表示一個類,就像css
一樣) - 您只需將它們放在一個數組中並將其傳遞給button()
方法: $(['#option1', '#option2', '#option3']).button('toggle');
這可以是類(以句點開頭,例如['.button-one', '.button-two']
等。
$(DOM ELEMENT).method(SCOPE)
語句,它們是 JS 使用的語法的更簡潔的形式,它會更冗長,比如: let lis = document.getElementsByTagName('li');
let firstLi = lis[0];
firstLi.addEventListener('click', () => console.log('clicked first li'))
所以它基本上是一樣的,只是不那么冗長。 因為 jquery 是專門為處理 DOM 開發的,所以這是有道理的。
如果您想在不加載 bootstrap buttons.js
庫的情況下實現相同的外觀,您可以將.active
類附加/刪除到.active
按鈕的標簽上,該類在附加時會使單選按鈕的外觀靜音,例如:
$('.btn').on('click', function () {
$(this).siblings('label').removeClass('active');
$(this).addClass('active');
});
聲明說明:當一個帶有.btn
類的元素被點擊時,它將從任何帶有標簽類型label
同級元素中刪除所有.active
類,並將.active
類添加到您點擊的.active
元素上。
但是,如果您檢查button.js
庫,您會發現toggle()
在幕后做了更多的事情,而不僅僅是應用/刪除.active
(並且對於不同的可能用例有很多很多條件。. .):
_proto.toggle = function toggle() {
var triggerChangeEvent = true;
var addAriaPressed = true;
var rootElement = $__default['default'](this._element).closest(SELECTOR_DATA_TOGGLES)[0];
if (rootElement) {
var input = this._element.querySelector(SELECTOR_INPUT);
if (input) {
if (input.type === 'radio') {
if (input.checked && this._element.classList.contains(CLASS_NAME_ACTIVE)) {
triggerChangeEvent = false;
} else {
var activeElement = rootElement.querySelector(SELECTOR_ACTIVE);
if (activeElement) {
$__default['default'](activeElement).removeClass(CLASS_NAME_ACTIVE);
}
}
}
if (triggerChangeEvent) {
// if it's not a radio button or checkbox don't add a pointless/invalid checked property to the input
if (input.type === 'checkbox' || input.type === 'radio') {
input.checked = !this._element.classList.contains(CLASS_NAME_ACTIVE);
}
if (!this.shouldAvoidTriggerChange) {
$__default['default'](input).trigger('change');
}
}
input.focus();
addAriaPressed = false;
}
}
if (!(this._element.hasAttribute('disabled') || this._element.classList.contains('disabled'))) {
if (addAriaPressed) {
this._element.setAttribute('aria-pressed', !this._element.classList.contains(CLASS_NAME_ACTIVE));
}
if (triggerChangeEvent) {
$__default['default'](this._element).toggleClass(CLASS_NAME_ACTIVE);
}
}
};
所以,可能最好只加載button.js
...
旁注:jquery 中的箭頭函數通常以window
對象為目標,因此最好避免,除非您知道自己在做什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.