简体   繁体   English

如何在HTML选择元素中的选项文本之前放置空格字符?

[英]How do I put a space character before option text in a HTML select element?

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? 如何在选项文本之前添加空格?

Isn't &#160 the entity for space? &#160是空间的实体?

<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; 只是想我应该让你知道,因为你可能想要替换为&nbsp;

Use \\xA0 with String. \\ xA0与String 一起使用。 This Works Perfect while binding C# Model Data to a Dropdown... 这项工作完美,同时将C#模型数据绑定到下拉列表...

  SectionsList.ForEach(p => { p.Text = "\xA0\xA0Section: " + p.Text; });

I think you want &nbsp; 我想你想要&nbsp; or &#160; &#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>

As Rob Cooper pointed out, there are some compatibility issues with older browsers (IE6 will display the actual letters "& nbsp;" 正如Rob Cooper指出的那样,旧浏览器存在一些兼容性问题(IE6会显示实际的字母“&nbsp;”

This is how I get around it in ASP.Net (I don't have VS open so I'm not sure what characters this actually gets translated to): 这就是我在ASP.Net中解决这个问题的方法(我没有打开VS所以我不确定这些字符实际上被翻译成了什么):

Server.HtmlDecode("&nbsp;") 

i tried multiple things but the only thing that worked for me was to use javascript. 我尝试了很多东西,但唯一对我有用的是使用javascript。 just notice that i'm using the unicode code for space rather than the html entity, as js doenst know a thing about entities 只是注意到我使用的是unicode代码用于空间而不是html实体,因为js doenst知道关于实体的事情

$("#project_product_parent_id option").each(function(i,option){
  $option = $(option);
  $option.text($option.text().replace(/─/g,'\u00A0\u00A0\u00A0'))
});

You can also press alt+space (on mac) for a non-breaking space. 您也可以按alt + space(在Mac上)以获得不间断的空间。 I use it for a Drupal module because Drupal decodes html entities. 我将它用于Drupal模块,因为Drupal解码了html实体。

I'm nearly certain you can accomplish this with CSS padding, as well. 我几乎可以肯定你也可以通过CSS填充实现这一点。 Then you won't be married to the space characters being hard-coded into all of your <option> tags. 然后,您将不会与硬编码到所有<option>标签中的空格字符结婚。

@Brian @布赖恩

I'm nearly certain you can accomplish this with CSS padding, as well. 我几乎可以肯定你也可以通过CSS填充实现这一点。 Then you won't be married to the space characters being hard-coded into all of your tags. 然后,您将不会与硬编码到所有标签中的空格字符结婚。

Good thinking - but unfortunately it doesn't work in (everyone's favourite browser...) IE7 :-( 好的想法 - 但不幸的是它不起作用(每个人最喜欢的浏览器......)IE7 :-(

Here's some code that will work in Firefox (and I assume Op/Saf). 这里有一些代码可以在Firefox中使用(我假设Op / Saf)。

<select>
    <option style="padding-left: 0px;">Blah</option>
        <option style="padding-left: 5px;">Blah</option>
            <option style="padding-left: 10px;">Blah</option>
    <option style="padding-left: 0px;">Blah</option>
        <option style="padding-left: 5px;">Blah</option>
</select>

只需使用char 255(在数字键盘上输入Alt + 2 + 5 + 5 )和Courier New等等宽字体。

&nbsp;

Can you try that? 你能试试吗? Or is it the same? 还是一样吗?

Server.HtmlDecode("&nbsp;") is the only one that worked for me. Server.HtmlDecode("&nbsp;")是唯一适合我的。

Otherwise the chr are printed as text. 否则chr将打印为文本。

I tried to add the padding as a Attribute for the listitem, however it didnt affect it. 我试图将填充添加为listitem的属性,但它并没有影响它。

I was also having the same issue and I was required to fix this as soon as possible. 我也有同样的问题,我被要求尽快解决这个问题。 Though I googled a lot, I was not able to find a quick solution. 虽然我搜索了很多,但我找不到快速的解决方案。

Instead I used my own solution, though I am not sure if its appropriate one, it works in my case and exactly which I was required to do. 相反,我使用了自己的解决方案,虽然我不确定它是否合适,但它适用于我的情况以及我需要做的事情。

So when you add an ListItem in dropdown and you want to add space then use the following:- 因此,当您在下拉列表中添加ListItem并且想要添加空间时,请使用以下内容: -

Press ALT and type 0160 on your numeric keypad, so it should be something like ALT+0160 . ALT键并在数字键盘上键入0160 ,因此它应该类似于ALT+0160 It will add a space. 它会增加一个空间。

ListItem("ALT+0160 ALT+0160 TEST", "TESTVAL")

In PHP you can use html_entity_decode: 在PHP中,您可以使用html_entity_decode:

obj_dat.options[0] = new Option('Menu 1', 'Menu 1');
obj_dat.options[1] = new Option('<?php echo html_entity_decode('&nbsp;&nbsp;&nbsp;'); ?>Menu 1.1', 'Menu 1.1');

I tried several of these examples, but the only thing that worked was using javascript, much like dabobert's, but not jQuery, just plain old vanilla javascript and spaces: 我尝试了几个这样的例子,但唯一有用的是使用javascript,就像dabobert的,但不是jQuery,只是简单的旧vanilla javascript和空格:

for(var x = 0; x < dd.options.length; x++)
{
    item = dd.options[x];
    //if a line that needs indenting
    item.text = '     ' + item.text; //indent
}

This is IE only. 这只是IE浏览器。 IE11 in fact. 事实上IE11。 Ugh. 啊。

1.Items Return to List 1.Items返回列表

2.Foreach loop in list 2.Foreach循环列表

3.. 3 ..

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

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

This work for me!!! 这项工作对我来说!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM