我正在努力实现以下目标:
<select ...>
<option>Column 1 Column 2</option>
<option>Line 1 Data 1</option>
<option>Line 2 Data 2</option>
<option>Line 3 Data 3</option>
<option>... ...</option>
<option>Line n Data n</option>
</select>
不使用固定宽度的字体。我有一个选项 + 描述,我想为 <select multiple />
中的每个选项显示.
直接使用 css/html 是否可行,还是我需要寻找插件?
最佳答案
第一个片段适用于双列选择列表,而第二个片段可以处理多列。分号 ;
用作分隔符。
// two-column multiple select list
window.onload = function(){
var s = document.getElementsByTagName('SELECT')[0].options,
l = 0,
d = '';
for(i = 0; i < s.length; i++){
if(s[i].text.length > l) l = s[i].text.length;
}
for(i = 0; i < s.length; i++){
d = '';
line = s[i].text.split(';');
l1 = (l - line[0].length);
for(j = 0; j < l1; j++){
d += '\u00a0';
}
s[i].text = line[0] + d + line[1];
}
};
工作 jsBin
// multiple-column multiple select list
window.onload = function(){
var s = document.getElementsByTagName('SELECT')[1].options, l = [];
for(i = 0; i < s.length; i++){
column = s[i].text.split(';');
for(j = 0; j < column.length; j++){
if(!l[j]) l[j] = 0;
if(column[j].length > l[j]){
l[j] = column[j].length;
}
}
}
for(i = 0; i < s.length; i++){
column = s[i].text.split(';');
temp_line = '';
for(j = 0; j < column.length; j++){
t = (l[j] - column[j].length);
d = '\u00a0';
for(k = 0; k < t; k++){
d += '\u00a0';
}
temp_line += column[j] + d;
}
s[i].text = temp_line;
}
};
工作 jsBin
关于javascript - 选择带有列的多个 Html - 可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25213160/