我在html中有一个select标记,我在javascript/jquery中追加了选项。
问题是内容是动态的,它的价格金额带利息。
我想要左边的价格和右边的利息,但我不知道怎么弄清楚。
在本例中,我创建了硬编码的空白,如您所见,这看起来非常奇怪:
有没有办法把第二列一直往右扔,并保持它对齐?
谢谢。
发布于 2020-02-05 21:02:25
以下是使用单空间字体的可能解决方案:
const wordPairs = [
['hello', 'world'],
['hello', 'there!'],
['why', 'hello!!!'],
['hey', 'bro'],
['hi', '...']
];
const maxChars = Math.max(...wordPairs.map(pair => pair.join('').length));
const minDistance = 5;
const targetLength = maxChars + minDistance;
const select = document.getElementById('select');
for (const pair of wordPairs) {
const option = document.createElement('option');
const texts = [pair[0], '', pair[1]];
while (texts.join('').length < targetLength) {
texts[1] += String.fromCharCode(160);
}
option.innerHTML = texts.join('');
select.appendChild(option);
}
<select id="select" style="font-family: monospace">
</select>
发布于 2020-02-05 20:43:17
如果您使用像Ariel这样的字体,其中所有的字母和空格都是相同的wifth,您可以简单地计算字符的数量并添加所需的间距。您将需要知道左字的选择宽度-右字的宽度。
https://stackoverflow.com/questions/60083668
复制相似问题