gpt4 book ai didi

jquery-ui:如何在选择菜单项(选项)中设置类名?

转载 作者:行者123 更新时间:2023-12-01 07:40:46 24 4
gpt4 key购买 nike

我有一个<select>为一个选项设置唯一类的形式:

<select id='mylist'>
<option value = '1'>One</option>
<option value = '2' class='my_element'>Two</option>
</select>

当我连接时selectmenu

$('#mylist').selectmenu();

新类 selectmenu对象消失(不出现)

<li class="ui-menu-item"><div id="ui-id-3" tabindex="-1" role="option" class="ui-menu-item-wrapper">Two</div></li>

如何解决?我想要

<li class="ui-menu-item"><div id="ui-id-3" tabindex="-1" role="option" class="ui-menu-item-wrapper my_element">Two</div></li>

或者

<li class="ui-menu-item my_element"><div id="ui-id-3" tabindex="-1" role="option" class="ui-menu-item-wrapper">Two</div></li>

最佳答案

您可以覆盖_renderMenu() :

$('#mylist').selectmenu().data("ui-selectmenu")._renderMenu = function(ul, items) {
var that = this;
items.forEach(function(ele, idx) {
var li = that._renderItemData(ul, ele);
if (ele.element.get(0).classList.length>0) {
// preserve original classes...
li.addClass(ele.element.get(0).classList.value);
}
});
};
.my_element {
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>

<select id='mylist'>
<option value='1'>One</option>
<option value='2' class='my_element'>Two</option>
</select>

关于jquery-ui:如何在选择菜单项(选项)中设置类名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49429103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com