gpt4 book ai didi

javascript - 选项 HTML,更改 SelectBox 插件的默认结构

转载 作者:行者123 更新时间:2023-12-03 10:07:23 25 4
gpt4 key购买 nike

我对这个编码世界有点陌生,所以对我来说这看起来很简单,但我无法解决它。给我这样痛苦的功能是

function _renderOptions() {
var optionHTML = [];

$options.each(function(i, itm) {
var $this = $(this),
optgroup = $this.parents('optgroup'),
addlOptClasses = "",
iconMarkup = "";

// render optgroups if present in original select
if (optgroup.length > 0 && $this.prev().length === 0) {
optionHTML.push('<dt>' + optgroup.attr('label') + '</dt>');
}

// if option has a classname add that to custom select as well
if (itm.className !== "") {
$(itm.className.split(" ")).each(function() {
iconMarkup += '<span class="' + this + '"></span>';
});
}

// add selected class to whatever option is currently active
if (itm.selected && !itm.disabled) {
_selectedValue = iconMarkup + _truncate($(itm).html());
addlOptClasses = " " + SELECTED_CLASS;
}

// Check for disabled options
if (itm.disabled) {
addlOptClasses += " " + DISABLED_CLASS;
}

optionHTML.push('<dd class="itm-' + i + ' ' + addlOptClasses + '">' + iconMarkup + itm.innerHTML + '</dd>');
});

if ($selectedValue && $selectedValue.get(0) !== null) {
$selectedValue.html(_selectedValue);
}

return optionHTML.join("");
}

我想要的是,如果选项有一个类名,则将相同的类添加到 <dd> 上的类中而不是创建<span>并在那里添加类

最佳答案

从项目中获取类并将其添加到 dd元素:optionHTML.push('<dd class="itm-' + i + ' ' + addlOptClasses + ' ' + $(itm).attr("class") + '">' + iconMarkup + itm.innerHTML + '</dd>');

function _renderOptions() {
var optionHTML = [];

$options.each(function(i, itm) {
var $this = $(this),
optgroup = $this.parents('optgroup'),
addlOptClasses = "",
iconMarkup = "";

// render optgroups if present in original select
if (optgroup.length > 0 && $this.prev().length === 0) {
optionHTML.push('<dt>' + optgroup.attr('label') + '</dt>');
}

// if option has a classname add that to custom select as well
if (itm.className !== "") {
$(itm.className.split(" ")).each(function() {
iconMarkup += '<span class="' + this + '"></span>';
});
}

// add selected class to whatever option is currently active
if (itm.selected && !itm.disabled) {
_selectedValue = iconMarkup + _truncate($(itm).html());
addlOptClasses = " " + SELECTED_CLASS;
}

// Check for disabled options
if (itm.disabled) {
addlOptClasses += " " + DISABLED_CLASS;
}

optionHTML.push('<dd class="itm-' + i + ' ' + addlOptClasses + ' ' + $(itm).attr("class") + '">' + iconMarkup + itm.innerHTML + '</dd>');
});

if ($selectedValue && $selectedValue.get(0) !== null) {
$selectedValue.html(_selectedValue);
}

return optionHTML.join("");
}

关于javascript - 选项 HTML,更改 SelectBox 插件的默认结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30299062/

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