问题
My problem comes from HTML dropdown select not able to preserve multiple consecutive whitespaces in its options. Here is a sample testcase and outcome. The "white-space:pre" class css seems to work only for any html element except the select element options. Is there any literature or cases identifying such issue? Any suggestions/ advise helpful
print "<style>.keepwhitespace { white-space: pre }</style>
<p class='keepwhitespace'>abc def</p>
abc def
<br/>
<br/>select and options with keepwhitespace
<select onchange=\"alert('\''+this.value+'\' '+this.value.length)\" class='keepwhitespace'>
<option class='keepwhitespace'>Any </option>
<option class='keepwhitespace'>abc def </option>
<option class='keepwhitespace'> Any </option>
<option class='keepwhitespace'>abc def </option>
</select>
<br/>select with keepwhitespace
<select onchange=\"alert('\''+this.value+'\' '+this.value.length)\" class='keepwhitespace'>
<option >Any </option>
<option >abc def </option>
<option> Any </option>
<option>abc def </option>
</select>
<br/>options with keepwhitespace
<select onchange=\"alert('\''+this.value+'\' '+this.value.length)\">
<option class='keepwhitespace'>Any </option>
<option class='keepwhitespace'>abc def </option>
<option class='keepwhitespace'> Any </option>
<option class='keepwhitespace'>abc def </option>
</select>";
outcome looks like this:
preloaded As can be seen in the "p" element tag (and any other that I tried using, worked well formatting text with the "whitespace" css style. Yet the select does not.

on selection to view the item value string and length of string (the length SHOULD have been 10 instead of 7, 'abc def' is 8 chars in length)

LUHFLUH
回答1:
Form inputs usually map to their relevant native OS controls so they can be hard to style. The usual workaround to keep whitespace from collapsing into 1 space in <option>
s is to use
instead of usual spaces.
eg. one of your options would be:
<option> Any </option>
For more custom appearance, you would hide the real select element completely and replace it with custom markup and javascript.
回答2:
The accepted answer is correct when dynamically creating the SELECT list from a server-side language. However, if you would like to create the options list in Javascript using the document.createElement function (probably the safest way to do so when using input you can't always completely control) then you can use the following method:
var someSelectElement = document.getElementById("id_of_select_element");
var optionSt = "Some spaces go here.";
var tObj = document.createElement("OPTION");
tObj.value = 1; // whatever value you need.
tObj.title = "Whatever title you want to appear when hovering over the option";
tObj.text = decodeURI(optionSt.replace(/ /g, "%C2%A0")); // decodeURI is the key here.
// Then add it to an already existing HTML select element...
someSelectElement.appendChild(tObj);
来源:https://stackoverflow.com/questions/11293536/preserve-whitespace-in-html-select-element-options-using-white-space-pre-not