如果内容超过使用 JavaScript 的 html 表的固定宽度,则将文本拆分为下一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14135962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
split text into the next line if content exceeds the fixed width for html table using JavaScript
提问by Human Being
I have a div, which consists of table with different styles applied td.
我有一个 div,它由应用 td 的不同样式的表组成。
I am trying to implement a layout, if the text exceeds the width of the table td, it should go over to the next line.
我正在尝试实现一个布局,如果文本超过了表格 td 的宽度,它应该转到下一行。
But I can't do this. My code is
但我不能这样做。我的代码是
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>HTML Div Overflow scrollbars</title>
<style type="text/css">
.mainDiv {
height: 230px;
width: 600px;
overflow: scroll;
}
.divScroll-1 {
width: 70%;
white-space:nowrap;
overflow-y: scroll;
}
.divScroll-2 {
width: 30%;
float:right;
white-space:nowrap;
}
</style>
<script>
function getText() {
var str = document.getElementById("no").value;
var div = document.getElementById("divScroll-1");
var ss = document.getElementById("div33");
var textarea = document.getElementById("mainDiv");
if (div.innerHTML == "" ) {
div.innerHTML += str ;
} else {
div.innerHTML += "</br>" + str ;
}
if(ss.innerHTML == ""){
ss.innerHTML = "Time" ;
}else{
ss.innerHTML += "</br>"+"Time";
}
}
</script>
</head>
<body>
<h3>Vertical Overflow Scroll</h3>
<table align="center" width="100%" border=1>
<tr>
<td>
<div class="mainDiv" id="mainDiv">
<table width="100%" align="left" style="table-layout:fixed">
<tr>
<td height="200px" style="max-width:150px;" valign="top" class="divScroll-1" id="divScroll-1">
</td>
<td valign="top" class="divScroll-2" id="div33">
</td>
</tr>
</table>
</div>
</td></tr>
<tr>
<td>
<input type="text" id="no" size="20" />
<input type="button" onclick="getText()" value="Insert Text" />
</td></tr></table>
</body>
</html>
I am appending the text into the table using Javascript. If I insert a long text bigger than the width of td, remaining text will be in hidden.
我正在使用 Javascript 将文本附加到表格中。如果我插入一个大于 td 宽度的长文本,剩余的文本将被隐藏。
I need the remaining content in to the next line.
我需要将剩余的内容放到下一行。
Please give some suggestions or correct my code.
请给出一些建议或更正我的代码。
回答by ahmadkwench
Try this, already on stackoverflow
试试这个,已经在 stackoverflow
CSS: How do I wrap text with no whitespace inside a <td>?
update your css class as follows ("divScroll-1")
更新你的css类如下(“divScroll-1”)
.divScroll-1 {
white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
white-space: pre-wrap; /* css-3 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
word-break: break-all;
white-space: normal;
}
回答by Mark Dee
remove white-space: nowrap;in your css.
white-space: nowrap;在你的 css 中删除。

