这个问题在这里已经有了答案:
9年前关闭。
Possible Duplicate:
Word wrap options in a select list
是否可以在 html 中换行长文本
<option>
盒子?在我的 JSP 中,我有一段代码在循环中从数据库中提取值并将它们附加到
<option>
<select>
的部分列表。以下是我引用的代码部分:
<select name='codes' onchange="showState(this.value)">
<option value="none">Select a code</option>
<%
//Pulls the ids and descriptions from the codes table and stores them in the first drop down
try
{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection con = DriverManager.getConnection("url","username","password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select id, descr from ref_codes");
while(rs.next())
{
%>
<option value="<%=rs.getString(1)%>"><%=rs.getString(1)%> <%=rs.getString(2)%></option>
<%
}
//Closes the database connection
stmt.close();
con.close();
}
catch (ClassNotFoundException e)
{
System.err.println("ClassNotFoundException: " + e.getMessage());
}
catch (SQLException e)
{
System.err.println("SQLException: " + e.getMessage());
}
catch (Exception e)
{
System.err.println("Generic Exception: " + e.getMessage());
}
%>
</select>
我的问题是某些拉取的值太长了,并且下拉框的长度超过了页面的长度,因为它们都保持在同一行。每当我遇到一个很长的字符串时,我都想让它换行到下一行,但仍然包含在同一
<option>
中值(value)。我在谷歌上环顾四周,得到了不同的答案。有人说可以,有人说不能。
但是,大多数答案似乎已经过时(大约从 2000 年代中期开始)和我尝试过的答案,主要涉及使用
<div>
标签,我没有运气。这意味着无论我指定 width
选项有多长,选项仍然保留在一行上并且不会换行到下一行。的<div>
标签。那么之前有没有人在 html 中做过这个或者知道这是否可能?
最佳答案
你不能只为下拉列表拉出你想要的最大字符数,然后重新选择相同的字段,但从下一个字符开始,抓取相同的字符数等。然后使用 HTML/CSS 格式来结合并使它们看起来像你想要的。
关于html - 如何在 HTML 下拉列表中换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12693420/