$(function () { $(".dropdown-menu.dropdown-select-menu").each((index, value) => { setDropdownItem(value) }) }) /** * fn 定義 | 手動初始化 Bootstrap dropdown select */ $.fn.droSetItem = function () { setDropdownItem(this); return this; } /** * 設置 bootstrap dropdown 為下拉選單 * @param {any} menuEle .dropdown-menu element */ function setDropdownItem(menuEle) { if ($(menuEle).find(".dropdown-item.active").length == 0) { $(menuEle).find(".dropdown-item").first().addClass("active"); } let actText = $(menuEle).find(".dropdown-item.active").first().text(); let actEleId = $(menuEle).prop("id"); $(`.dropdown-toggle[data-target=${actEleId}]`).text(actText); $(menuEle).trigger("active:change", $(menuEle).find(".dropdown-item.active")); //點選選項 add active class onEvent("click", ".dropdown-menu.dropdown-select-menu .dropdown-item", function () { $(this).parent(".dropdown-menu.dropdown-select-menu").find(".dropdown-item").removeClass("active"); $(this).addClass("active"); setDropdownItem($(this).parent(".dropdown-menu.dropdown-select-menu")); }) } /** * 預設設備圖像 * @param {any} obj */ function defDev(obj) { let defSrc = 'img/defdev.png'; obj.src = defSrc; } function creEle(ele,text) { return $(`<${ele}>${text}`); }