我有一个<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>
OR
<li class="ui-menu-item my_element"><div id="ui-id-3" tabindex="-1" role="option" class="ui-menu-item-wrapper">Two</div></li>
答案 0 :(得分:3)
您可以覆盖_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);
}
});
};
&#13;
.my_element {
background-color: red;
}
&#13;
<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>
&#13;
答案 1 :(得分:0)