HTML选项中的换行符?

时间:2022-08-24 11:17:33

Can I have a two line text in an html select option? How?

我可以在html选项中使用双行文本吗?怎么样?

8 个解决方案

#1


26  

No, browsers don't provide this formatting option.

不,浏览器不提供此格式化选项。

You could probably fake it with some checkboxes with <label>s, and JS to turn it into a fly out menu.

你可以用一些带有

#2


34  

I know this is an older post, but I'm leaving this for whomever else comes looking in the future.

我知道这是一篇较老的帖子,但是我将其留给未来的其他任何人。

You can't format line breaks into an option; however, you can use the title attibute to give a mouse-over tooltip to give the full info. Use a short descriptor in the option text and give the full skinny in the title.

您不能将换行符格式化为选项;但是,您可以使用标题attibute来提供鼠标悬停工具提示以提供完整信息。在选项文本中使用短描述符,并在标题中给出完整的skinny。

<option value="1" title="This is my lengthy explanation of what this selection really means, so since you only see 1 on the drop down list you really know that you're opting to elect me as King of Willywarts!  Always be sure to read the fine print!">1</option>

#3


10  

A bit of a hack, but this gives the effect of a multi-line select, puts in a gray bgcolor for your multi line, and if you select any of the gray text, it selects the first of the grouping. Kinda clever I'd say :) The first option also shows how you can put a title tag in for an option as well.

有点破解,但这会产生多行选择的效果,为多行输入灰色bgcolor,如果选择任何灰色文本,它会选择第一个分组。有点聪明我会说:)第一个选项还显示了如何为标题添加标题。

 function SelectFirst(SelVal) {
   var arrSelVal = SelVal.split(",")
   if (arrSelVal.length > 1) {
     Valuetoselect = arrSelVal[0];
     document.getElementById("select1").value = Valuetoselect;
   }
 }
<select name="select1" id="select1" onchange="SelectFirst(this.value)">
  <option value="1" title="this is my long title for the yes option">Yes</option>
  <option value="2">No</option>
  <option value="2,1" style="background:#eeeeee">&nbsp;&nbsp;&nbsp;This is my description for the no option</option>
  <option value="2,2" style="background:#eeeeee">&nbsp;&nbsp;&nbsp;This is line 2 for the no option</option>
  <option value="3">Maybe</option>
  <option value="3,1" style="background:#eeeeee">&nbsp;&nbsp;&nbsp;This is my description for Maybe option</option>
  <option value="3,2" style="background:#eeeeee">&nbsp;&nbsp;&nbsp;This is line 2 for the Maybe option</option>
  <option value="3,3" style="background:#eeeeee">&nbsp;&nbsp;&nbsp;This is line 3 for the Maybe option</option>
</select>

#4


7  

How about putting the long text in another <option> right below and disabling it? Might be a workaround for someone so posting here.

如何将长文本放在下面的另一个

        <select>
            <option>My real option text</option>
            <option disabled style="font-style:italic">&nbsp;&nbsp;&nbsp;(...and my really really long option text that is basically a continuation of previous option)</option> 
            <option disabled style="font-style:italic">&nbsp;&nbsp;&nbsp;(...and still continuing my previous option)</option> 
            <option>Another real option text</option>
        </select>

#5


6  

HTML Code

HTML代码

<section style="background-color:rgb(237.247.249);">
<h2>Test of select menu (SelectboxIt plugin)</h2>
<select name="select_this" id="testselectset">
    <option value="01">Option 1</option>
    <option value="02">Option 2</option>
    <option value="03">Option 3</option>
    <option value="04">Option 4</option>
    <option value="05">Option 5</option>
    <option value="06">Option 6</option>
    <option value="07">Option 7 with a really, really long text line that we shall use in order to test the wrapping of text within an option or optgroup</option>
    <option value="08">Option 8</option>
    <option value="09">Option 9</option>
    <option value="10">Option 10</option>
</select>
</section>

Javascript Code

Javascript代码

    $(function(){

        $("#testselectset").selectBoxIt({
            theme: "default",
            defaultText: "Make a selection...",
            autoWidth: false
        });


    });

CSS Code

CSS代码

.selectboxit-container .selectboxit, .selectboxit-container .selectboxit-options {
  width: 400px; /* Width of the dropdown button */
  border-radius:0;
  max-height:100px;
}

.selectboxit-options .selectboxit-option .selectboxit-option-anchor {
  white-space: normal;
  min-height: 30px;
  height: auto;
}

and you have to add some Jquery Library select Box Jquery CSS

你必须添加一些Jquery库选择Box Jquery CSS

Jquery Ui Min JS

Jquery Ui Min JS

SelectBox Js

SelectBox Js

Please check this link JsFiddle Link

请检查此链接JsFiddle Link

#6


2  

Does not work fully (the hr line part) on all browsers, but here is the solution:

在所有浏览器上都不能完全运行(hr行部分),但这是解决方案:

<select name="selector">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
  <option value="3">Option 3</option>
  <option disabled><hr></option>
  <option value="4">Option 4</option>
  <option value="5">Option 5</option>
  <option value="6">Option 6</option>
</select>

#7


0  

Instead, maybe try replacing the select with markup, e.g.

相反,也许尝试用标记替换选择,例如

// Wrap any selects that should be replaced
$('select.replace').wrap('<div class="select-replacement-wrapper"></div>');

// Replace placeholder text with select's initial value
$('.select-replacement-wrapper').each(function() {
	$(this).prepend('<a>' + $('select option:selected', this).text() + '</a>');
});

// Update placeholder text with updated select value
$('select.replace').change(function(event) {
  $(this).siblings('a').text( $('option:selected', this).text() );
});
/* Position relative, so that we can overlay the hidden select */
.select-replacement-wrapper {
  position: relative;
  border: 3px solid red; /* to show area */
  width: 33%;
  margin: 0 auto;
}

/* Only shows if JS is enabled */
.select-replacement-wrapper a {
  /* display: none; */
  /* Notice that we've centered this text - 
   you can do whatever you want, mulitple lines wrap, etc, 
   since this is not a select element' */
  text-align: center;
  display: block;
  border: 1px solid blue;
}

select.replace {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  border: 1px solid green;
  opacity: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>


<select id="dropdown" class="replace">
  <option value="First">First</option>
  <option value="Second" selected>Second, and this is a long line, just to show wrapping</option>
  <option value="Third">Third</option>
</select>

#8


-1  

yes, by using css styles white-space: pre-wrap; in the .dropdown class of the bootstrap by overriding it. Earlier it is white-space: nowrap; so it makes the dropdown wrapped into one line. pre-wrap makes it as according to the width.

是的,通过使用css样式white-space:pre-wrap;在引导程序的.dropdown类中通过覆盖它。早先它是白色空间:nowrap;所以它使下拉包装成一行。预包装使其成为根据宽度。

#1


26  

No, browsers don't provide this formatting option.

不,浏览器不提供此格式化选项。

You could probably fake it with some checkboxes with <label>s, and JS to turn it into a fly out menu.

你可以用一些带有

#2


34  

I know this is an older post, but I'm leaving this for whomever else comes looking in the future.

我知道这是一篇较老的帖子,但是我将其留给未来的其他任何人。

You can't format line breaks into an option; however, you can use the title attibute to give a mouse-over tooltip to give the full info. Use a short descriptor in the option text and give the full skinny in the title.

您不能将换行符格式化为选项;但是,您可以使用标题attibute来提供鼠标悬停工具提示以提供完整信息。在选项文本中使用短描述符,并在标题中给出完整的skinny。

<option value="1" title="This is my lengthy explanation of what this selection really means, so since you only see 1 on the drop down list you really know that you're opting to elect me as King of Willywarts!  Always be sure to read the fine print!">1</option>

#3


10  

A bit of a hack, but this gives the effect of a multi-line select, puts in a gray bgcolor for your multi line, and if you select any of the gray text, it selects the first of the grouping. Kinda clever I'd say :) The first option also shows how you can put a title tag in for an option as well.

有点破解,但这会产生多行选择的效果,为多行输入灰色bgcolor,如果选择任何灰色文本,它会选择第一个分组。有点聪明我会说:)第一个选项还显示了如何为标题添加标题。

 function SelectFirst(SelVal) {
   var arrSelVal = SelVal.split(",")
   if (arrSelVal.length > 1) {
     Valuetoselect = arrSelVal[0];
     document.getElementById("select1").value = Valuetoselect;
   }
 }
<select name="select1" id="select1" onchange="SelectFirst(this.value)">
  <option value="1" title="this is my long title for the yes option">Yes</option>
  <option value="2">No</option>
  <option value="2,1" style="background:#eeeeee">&nbsp;&nbsp;&nbsp;This is my description for the no option</option>
  <option value="2,2" style="background:#eeeeee">&nbsp;&nbsp;&nbsp;This is line 2 for the no option</option>
  <option value="3">Maybe</option>
  <option value="3,1" style="background:#eeeeee">&nbsp;&nbsp;&nbsp;This is my description for Maybe option</option>
  <option value="3,2" style="background:#eeeeee">&nbsp;&nbsp;&nbsp;This is line 2 for the Maybe option</option>
  <option value="3,3" style="background:#eeeeee">&nbsp;&nbsp;&nbsp;This is line 3 for the Maybe option</option>
</select>

#4


7  

How about putting the long text in another <option> right below and disabling it? Might be a workaround for someone so posting here.

如何将长文本放在下面的另一个

        <select>
            <option>My real option text</option>
            <option disabled style="font-style:italic">&nbsp;&nbsp;&nbsp;(...and my really really long option text that is basically a continuation of previous option)</option> 
            <option disabled style="font-style:italic">&nbsp;&nbsp;&nbsp;(...and still continuing my previous option)</option> 
            <option>Another real option text</option>
        </select>

#5


6  

HTML Code

HTML代码

<section style="background-color:rgb(237.247.249);">
<h2>Test of select menu (SelectboxIt plugin)</h2>
<select name="select_this" id="testselectset">
    <option value="01">Option 1</option>
    <option value="02">Option 2</option>
    <option value="03">Option 3</option>
    <option value="04">Option 4</option>
    <option value="05">Option 5</option>
    <option value="06">Option 6</option>
    <option value="07">Option 7 with a really, really long text line that we shall use in order to test the wrapping of text within an option or optgroup</option>
    <option value="08">Option 8</option>
    <option value="09">Option 9</option>
    <option value="10">Option 10</option>
</select>
</section>

Javascript Code

Javascript代码

    $(function(){

        $("#testselectset").selectBoxIt({
            theme: "default",
            defaultText: "Make a selection...",
            autoWidth: false
        });


    });

CSS Code

CSS代码

.selectboxit-container .selectboxit, .selectboxit-container .selectboxit-options {
  width: 400px; /* Width of the dropdown button */
  border-radius:0;
  max-height:100px;
}

.selectboxit-options .selectboxit-option .selectboxit-option-anchor {
  white-space: normal;
  min-height: 30px;
  height: auto;
}

and you have to add some Jquery Library select Box Jquery CSS

你必须添加一些Jquery库选择Box Jquery CSS

Jquery Ui Min JS

Jquery Ui Min JS

SelectBox Js

SelectBox Js

Please check this link JsFiddle Link

请检查此链接JsFiddle Link

#6


2  

Does not work fully (the hr line part) on all browsers, but here is the solution:

在所有浏览器上都不能完全运行(hr行部分),但这是解决方案:

<select name="selector">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
  <option value="3">Option 3</option>
  <option disabled><hr></option>
  <option value="4">Option 4</option>
  <option value="5">Option 5</option>
  <option value="6">Option 6</option>
</select>

#7


0  

Instead, maybe try replacing the select with markup, e.g.

相反,也许尝试用标记替换选择,例如

// Wrap any selects that should be replaced
$('select.replace').wrap('<div class="select-replacement-wrapper"></div>');

// Replace placeholder text with select's initial value
$('.select-replacement-wrapper').each(function() {
	$(this).prepend('<a>' + $('select option:selected', this).text() + '</a>');
});

// Update placeholder text with updated select value
$('select.replace').change(function(event) {
  $(this).siblings('a').text( $('option:selected', this).text() );
});
/* Position relative, so that we can overlay the hidden select */
.select-replacement-wrapper {
  position: relative;
  border: 3px solid red; /* to show area */
  width: 33%;
  margin: 0 auto;
}

/* Only shows if JS is enabled */
.select-replacement-wrapper a {
  /* display: none; */
  /* Notice that we've centered this text - 
   you can do whatever you want, mulitple lines wrap, etc, 
   since this is not a select element' */
  text-align: center;
  display: block;
  border: 1px solid blue;
}

select.replace {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  border: 1px solid green;
  opacity: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>


<select id="dropdown" class="replace">
  <option value="First">First</option>
  <option value="Second" selected>Second, and this is a long line, just to show wrapping</option>
  <option value="Third">Third</option>
</select>

#8


-1  

yes, by using css styles white-space: pre-wrap; in the .dropdown class of the bootstrap by overriding it. Earlier it is white-space: nowrap; so it makes the dropdown wrapped into one line. pre-wrap makes it as according to the width.

是的,通过使用css样式white-space:pre-wrap;在引导程序的.dropdown类中通过覆盖它。早先它是白色空间:nowrap;所以它使下拉包装成一行。预包装使其成为根据宽度。