admin管理员组文章数量:1426066
I am using IE7 and it seems to display options in my SELECT tag without the leading spaces in the text - the text seems to be trimmed. For example, in the HTML code block below, even though option 1 to 3 have three spaces in the front of its text, the browser when displaying them seems to perform a trim and displays it to the user without the leading spaces.
Code block 1:
<select size="3">
<option value="1"> aaa</option>
<option value="2"> bbb</option>
<option value="3"> ccc</option>
<option value="4">ddd</option>
<option value="5">eee</option>
</select>
It also seems that when you access the option's text through JavaScript, the returned value is also trimmed.
Code block 2:
var value = list.options[i].text;
Is there a way to force the browser to display the spaces as well or is this something I am stuck with?
I am using IE7 and it seems to display options in my SELECT tag without the leading spaces in the text - the text seems to be trimmed. For example, in the HTML code block below, even though option 1 to 3 have three spaces in the front of its text, the browser when displaying them seems to perform a trim and displays it to the user without the leading spaces.
Code block 1:
<select size="3">
<option value="1"> aaa</option>
<option value="2"> bbb</option>
<option value="3"> ccc</option>
<option value="4">ddd</option>
<option value="5">eee</option>
</select>
It also seems that when you access the option's text through JavaScript, the returned value is also trimmed.
Code block 2:
var value = list.options[i].text;
Is there a way to force the browser to display the spaces as well or is this something I am stuck with?
Share Improve this question asked Sep 12, 2011 at 6:23 methon.daggermethon.dagger 51310 silver badges28 bronze badges4 Answers
Reset to default 7Use
instead of spaces:
<select size="3">
<option value="1"> aaa</option>
<option value="2"> bbb</option>
<option value="3"> ccc</option>
<option value="4">ddd</option>
<option value="5">eee</option>
</select>
=== UPDATE ===
Also see my jsfiddle.
Did you try
in lieu of a space?
Not sure but try having  
; instead of just a space
It's browser behavior, removing white space from the option
text.. I found out that the innerHTML
is "preserved" though, so you can keep the HTML intact with the ordinary spaces then just add this JS:
window.onload = function() {
var dropDownLists = document.getElementsByTagName("select");
for (var i = 0; i < dropDownLists.length; i++) {
var oDDL = dropDownLists[i];
for (var j = 0; j < oDDL.options.length; j++) {
oDDL.options[j].innerHTML = oDDL.options[j].innerHTML.replace(/ /g, " ");
}
}
};
This will replace the space with its HTML entity for you.
Live test case.
本文标签: javascriptBrowser not displaying spaces of list text within SELECT tagStack Overflow
版权声明:本文标题:javascript - Browser not displaying spaces of list text within SELECT tag? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745458110a2659197.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论