如何设置宽度以视觉上截断其显示的内容?
https://stackoverflow.com/questions/480722
-
20-08-2019
- |
-
题
我正在尝试设置表格的列宽度,该列的工作正常,直到到达视觉上截断子元素的地步...然后它不会大小较小。 (“视觉截断” - 不合适的东西似乎隐藏在下一个列后面)
这是一些示例代码来证明我的问题:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script language="javascript" type="text/javascript">
var intervalID = null;
function btn_onClick()
{
// keep it simple, only click once
var btn = document.getElementById("btn");
btn.disabled = true;
// start shrinking
intervalID = window.setInterval( "shrinkLeftCell();", 100);
}
function shrinkLeftCell()
{
// get elements
var td1 = document.getElementById("td1");
var td2 = document.getElementById("td2");
// initialize for first pass
if( "undefined" == typeof( td1.originalWidth))
{
td1.originalWidth = td1.offsetWidth;
td1.currentShrinkCount = td1.offsetWidth;
}
// shrink and set width size
td1.currentShrinkCount--;
td1.width = td1.currentShrinkCount; // does nothing if truncating!
// set reporting values in right cell
td2.innerHTML = "Desired Width : ["
+ td1.currentShrinkCount
+ "], Actual : ["
+ td1.offsetWidth + "]";
// Stop shrinking
if( 1 >= td1.currentShrinkCount)
window.clearInterval(intervalID);
}
</script>
</head>
<body>
<table width="100%" border="1">
<tr>
<td id="td1">
Extra_long_filler_text
</td>
<td id="td2">
Desired Width : [----], Actual : [----]
</td>
</tr>
</table>
<button id="btn" onclick="btn_onClick();">Start</button>
</body>
</html>
我尝试以不同的方式设置宽度
td1.offsetWidth = td1.currentShrinkCount;
和
td1.width = td1.currentShrinkCount + "px";
和
td1.style.width = td1.currentShrinkCount + "px";
和
td1.style.posWidth = td1.currentShrinkCount;
和
td1.scrollWidth = td1.currentShrinkCount;
和
td1.style.overflow = "hidden";
td1.width = td1.currentShrinkCount;
和
td1.style.overflow = "scroll";
td1.width = td1.currentShrinkCount;
和
td1.style.overflow = "auto";
td1.width = td1.currentShrinkCount;
我意识到,我可能可以使用DIV,但是由于表是从界控件中生成的,因此我不想使用DIV,而且我真的不想进入重写ASP.NET控件。
话虽如此,我认为作为最后的沟渠努力,我至少可以用Div包裹每个TD1的内容,而溢出会照顾好事,甚至更换
<td id="td1">
Extra_long_filler_text
</td>
和
<td id="td1" style="overflow:hidden;">
<div style="margin=0;padding:0;overflow:hidden;">
Extra_long_filler_text
</div>
</td>
不起作用。
有人知道我如何设置宽度以视觉上截断其内容吗?
BTW-MY目标浏览器是IE7。其他浏览器现在并不重要,因为这是一个内部应用程序。
解决方案
其他提示
CSS解决方案
“截断”可能不是您要寻找的单词。您可能只想隐藏溢出的字符。如果是这种情况,请查看CSS-Property“ Overflow”,它将隐藏任何延伸超过其容器声明限制的内容。
td div.masker {
height:25px;
width:100px;
overflow:hidden;
}
<td>
<div class="masker">
This is a string of sample text that
should be hidden when it reaches out of the bounds of the parent controller.
</div>
</td>
JavaScript解决方案
如果您确实想截断容器中的文本,则必须从右侧删除一个字符,测试父的宽度,然后继续删除字符并测试父宽度,直到父宽度与已声明的宽度匹配。
while (parent.width > desiredWidth) {
remove one char from right-side of child-text
}
<td id="td1" style="overflow-x:hidden;">
应该
<td id="td1" style="overflow:hidden;">
Overflow-X是Microsoft CSS属性,现在是CSS3的一部分。我会坚持旧的溢出属性。
编辑:
正如Paolo提到的那样,您还需要添加表格:固定;并指定表的宽度。如下一个完整的示例。
<html>
<head>
<style>
table
{
table-layout:fixed;
width:100px;
}
td
{
overflow:hidden;
width:50px;
border-bottom: solid thin;
border-top:solid thin;
border-right:solid thin;
border-left:solid thin;
}
</style>
</head>
<body>
<table>
<tr><td>Some_long_content</td><td>Short</td></tr>
</table>
</body>
</html>
据我所知,桌子牢房总是会伸展以适应其内容物。
您是否考虑过使用JavaScript动态截断具有超过一定量内容的表单元格中的数据?另外,您可以更轻松地执行服务器端。
您可以使用jQuery或Plain JavaScript用DIV标签包围单元格的内容,并设置固定的宽度和溢出:而是隐藏在这些标签上。不漂亮,但它会起作用。
ETA:
<td><div style="width:30px;overflow:hidden">Text goes here</div></td>
由于DIV是边界元素,因此应设置宽度。
不隶属于 StackOverflow
https://stackoverflow.com/questions/480722
-
20-08-2019 - |
题
我正在尝试设置表格的列宽度,该列的工作正常,直到到达视觉上截断子元素的地步...然后它不会大小较小。 (“视觉截断” - 不合适的东西似乎隐藏在下一个列后面)
这是一些示例代码来证明我的问题:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script language="javascript" type="text/javascript">
var intervalID = null;
function btn_onClick()
{
// keep it simple, only click once
var btn = document.getElementById("btn");
btn.disabled = true;
// start shrinking
intervalID = window.setInterval( "shrinkLeftCell();", 100);
}
function shrinkLeftCell()
{
// get elements
var td1 = document.getElementById("td1");
var td2 = document.getElementById("td2");
// initialize for first pass
if( "undefined" == typeof( td1.originalWidth))
{
td1.originalWidth = td1.offsetWidth;
td1.currentShrinkCount = td1.offsetWidth;
}
// shrink and set width size
td1.currentShrinkCount--;
td1.width = td1.currentShrinkCount; // does nothing if truncating!
// set reporting values in right cell
td2.innerHTML = "Desired Width : ["
+ td1.currentShrinkCount
+ "], Actual : ["
+ td1.offsetWidth + "]";
// Stop shrinking
if( 1 >= td1.currentShrinkCount)
window.clearInterval(intervalID);
}
</script>
</head>
<body>
<table width="100%" border="1">
<tr>
<td id="td1">
Extra_long_filler_text
</td>
<td id="td2">
Desired Width : [----], Actual : [----]
</td>
</tr>
</table>
<button id="btn" onclick="btn_onClick();">Start</button>
</body>
</html>
我尝试以不同的方式设置宽度
td1.offsetWidth = td1.currentShrinkCount;
和
td1.width = td1.currentShrinkCount + "px";
和
td1.style.width = td1.currentShrinkCount + "px";
和
td1.style.posWidth = td1.currentShrinkCount;
和
td1.scrollWidth = td1.currentShrinkCount;
和
td1.style.overflow = "hidden";
td1.width = td1.currentShrinkCount;
和
td1.style.overflow = "scroll";
td1.width = td1.currentShrinkCount;
和
td1.style.overflow = "auto";
td1.width = td1.currentShrinkCount;
我意识到,我可能可以使用DIV,但是由于表是从界控件中生成的,因此我不想使用DIV,而且我真的不想进入重写ASP.NET控件。
话虽如此,我认为作为最后的沟渠努力,我至少可以用Div包裹每个TD1的内容,而溢出会照顾好事,甚至更换
<td id="td1">
Extra_long_filler_text
</td>
和
<td id="td1" style="overflow:hidden;">
<div style="margin=0;padding:0;overflow:hidden;">
Extra_long_filler_text
</div>
</td>
不起作用。
有人知道我如何设置宽度以视觉上截断其内容吗?
BTW-MY目标浏览器是IE7。其他浏览器现在并不重要,因为这是一个内部应用程序。
解决方案
其他提示
CSS解决方案
“截断”可能不是您要寻找的单词。您可能只想隐藏溢出的字符。如果是这种情况,请查看CSS-Property“ Overflow”,它将隐藏任何延伸超过其容器声明限制的内容。
td div.masker {
height:25px;
width:100px;
overflow:hidden;
}
<td>
<div class="masker">
This is a string of sample text that
should be hidden when it reaches out of the bounds of the parent controller.
</div>
</td>
JavaScript解决方案
如果您确实想截断容器中的文本,则必须从右侧删除一个字符,测试父的宽度,然后继续删除字符并测试父宽度,直到父宽度与已声明的宽度匹配。
while (parent.width > desiredWidth) {
remove one char from right-side of child-text
}
<td id="td1" style="overflow-x:hidden;">
应该
<td id="td1" style="overflow:hidden;">
Overflow-X是Microsoft CSS属性,现在是CSS3的一部分。我会坚持旧的溢出属性。
编辑:
正如Paolo提到的那样,您还需要添加表格:固定;并指定表的宽度。如下一个完整的示例。
<html>
<head>
<style>
table
{
table-layout:fixed;
width:100px;
}
td
{
overflow:hidden;
width:50px;
border-bottom: solid thin;
border-top:solid thin;
border-right:solid thin;
border-left:solid thin;
}
</style>
</head>
<body>
<table>
<tr><td>Some_long_content</td><td>Short</td></tr>
</table>
</body>
</html>
据我所知,桌子牢房总是会伸展以适应其内容物。
您是否考虑过使用JavaScript动态截断具有超过一定量内容的表单元格中的数据?另外,您可以更轻松地执行服务器端。
您可以使用jQuery或Plain JavaScript用DIV标签包围单元格的内容,并设置固定的宽度和溢出:而是隐藏在这些标签上。不漂亮,但它会起作用。
ETA:
<td><div style="width:30px;overflow:hidden">Text goes here</div></td>
由于DIV是边界元素,因此应设置宽度。