How do I put a space character before option text

2019-01-09 08:44发布

In a drop down list, I need to add spaces in front of the options in the list. I am trying

<select>
<option>&#32;&#32;Sample</option>
</select>

for adding two spaces but it displays no spaces. How can I add spaces before option texts?

14条回答
冷血范
2楼-- · 2019-01-09 09:12

I'm nearly certain you can accomplish this with CSS padding, as well. Then you won't be married to the space characters being hard-coded into all of your <option> tags.

查看更多
再贱就再见
3楼-- · 2019-01-09 09:13

I think you want &nbsp; or &#160;

So a fixed version of your example could be...

<select>
  <option>&nbsp;&nbsp;Sample</option>
</select>

or

<select>
  <option>&#160;&#160;Sample</option>
</select>
查看更多
萌系小妹纸
4楼-- · 2019-01-09 09:14

Use \xA0 with String. This Works Perfect while binding C# Model Data to a Dropdown...

  SectionsList.ForEach(p => { p.Text = "\xA0\xA0Section: " + p.Text; });
查看更多
Ridiculous、
5楼-- · 2019-01-09 09:20

You can also press alt+space (on mac) for a non-breaking space. I use it for a Drupal module because Drupal decodes html entities.

查看更多
甜甜的少女心
6楼-- · 2019-01-09 09:20

1.Items Return to List

2.Foreach loop in list

3..

foreach (var item in q)
{
    StringWriter myWriter = new StringWriter();

    myWriter.Lable = HttpUtility.HtmlDecode(item.Label.Replace(" ", "&nbsp;"));
}

This work for me!!!

查看更多
三岁会撩人
7楼-- · 2019-01-09 09:22

Isn't &#160 the entity for space?

<select>
<option>&#160;option 1</option>
<option>    option 2</option>
</select>

Works for me...

EDIT:

Just checked this out, there may be compatibility issues with this in older browsers, but all seems to work fine for me here. Just thought I should let you know as you may want to replace with &nbsp;

查看更多
登录 后发表回答