Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中剪切字符串,使其正好适合两行&;添加最后_Javascript_Jquery - Fatal编程技术网

如何在javascript中剪切字符串,使其正好适合两行&;添加最后

如何在javascript中剪切字符串,使其正好适合两行&;添加最后,javascript,jquery,Javascript,Jquery,我有一个要求,在我必须显示两行文字和添加。。。如果它溢出 我的div宽度是固定的(甚至高度也可以被认为是固定的) 实际文本如下所示: 1. Lorem Ipsum只是 打印的虚拟文本 以及排版业 2. Lorem Ipsum只是一个文本 打印和打印类型的名称 行业。 期望值: 1. Lorem Ipsum只是 打印的虚拟文本… 2. Lorem Ipsum只是一个文本 打印和打印的类型… 我不想让插件过载(三点jquery插件就是这么做的) 我计划在div宽度固定的情况下剪切(分割)字符串 提前

我有一个要求,在我必须显示两行文字和添加。。。如果它溢出 我的div宽度是固定的(甚至高度也可以被认为是固定的)

实际文本如下所示:

1. Lorem Ipsum只是
打印的虚拟文本
以及排版业

2. Lorem Ipsum只是一个文本
打印和打印类型的名称
行业。

期望值:

1. Lorem Ipsum只是
打印的虚拟文本…

2. Lorem Ipsum只是一个文本
打印和打印的类型…

我不想让插件过载(三点jquery插件就是这么做的)

我计划在div宽度固定的情况下剪切(分割)字符串


提前感谢。

更新:看起来像是
文本溢出
只对水平截断有用

这里有一个小jQuery应该可以工作

试试看:


您将无法获得完全的跨浏览器支持,但已关闭。IE6+、Safari、Konqueror和Opera(具有特定于Opera的属性)都支持名为
文本溢出的CSS属性


CSS文本溢出属性是最好的方法,但并非所有浏览器都支持它。如果您想使用Javascript,可以创建一个屏幕外元素。使用CSS(由脚本生成)为其提供与目标相同的宽度和字体,高度为“2米”(即2行)。运行循环,每次向其中添加一个字母,直到字母用完或高度发生变化。当高度增加时,您知道您已超过两行

当然,您还需要考虑“…”的宽度,因此您可以将其附加到要添加的字母中


确保元素在屏幕外,而不是使用“显示:无”,因为未显示的元素没有高度。“可见性:隐藏”可能有效;我还没有尝试过。

由于溢出省略号css不适用于多行文本,因此必须使用JavaScript。您可以复制文本框,将其放置在top-1000000px的绝对位置,并剪切文本的最后一个字母,直到框具有正确的高度。或者在特定的字母计数后简单剪切,这会更快,但不准确。

只是对用户113716的post的一个添加。 更清洁的方法:

var $container = $('#container');
var $text = $('#text');
var temp = $text.text();

if ($container.height() < $text.outerHeight()) {
  while($container.height() < $text.outerHeight()) {
    temp = temp.substr(0, temp.length-1)
    $text.text(temp + '...');
  }
}
var$container=$('#container');
var$text=$(“#text”);
var temp=$text.text();
如果($container.height()<$text.outerHeight()){
而($container.height()<$text.outerHeight()){
温度=温度子序列(0,温度长度-1)
$text.text(临时+“…”);
}
}

一个简单的方法是使用一个小小的jQuery插件

$.fn.trimToParentHeight = function(options) {
    options = $.extend($.fn.trimToParentHeight.defaults, options);

    return this.each(function(index, text) {
        text = $(text);
        var height = parseInt(text.parent().height());
        var temp = text.text();
        while(parseInt(text.outerHeight()) > height && temp.length>0) {
            temp = temp.substr(0, temp.length-1)
            text.text(temp +  options.ellipsis);
        }
    });
}

$.fn.trimToParentHeight.defaults = {
    ellipsis: '...'
};
然后你就可以做

$(".container .text").trimToParentHeight()
。。。只要容器是文本的直接父级

string="test/firstpage/secondpage/thirdpage";

string.split("/").slice(0, 1);

输出将是:测试

击败我。:)我已经读到IE6也支持这一点,但如果不亲眼看到,我很难相信。@D Hoerster-看起来它是IE6支持的。(我刚刚更新了。)我刚刚发布的内容链接显示了它的内容。请注意,firefox只支持hack(),所有浏览器只支持一行,因此您的示例不支持溢出省略号。
var $container = $('#container');
var $text = $('#text');
var temp = $text.text();

if ($container.height() < $text.outerHeight()) {
  while($container.height() < $text.outerHeight()) {
    temp = temp.substr(0, temp.length-1)
    $text.text(temp + '...');
  }
}
$.fn.trimToParentHeight = function(options) {
    options = $.extend($.fn.trimToParentHeight.defaults, options);

    return this.each(function(index, text) {
        text = $(text);
        var height = parseInt(text.parent().height());
        var temp = text.text();
        while(parseInt(text.outerHeight()) > height && temp.length>0) {
            temp = temp.substr(0, temp.length-1)
            text.text(temp +  options.ellipsis);
        }
    });
}

$.fn.trimToParentHeight.defaults = {
    ellipsis: '...'
};
$(".container .text").trimToParentHeight()
string="test/firstpage/secondpage/thirdpage";

string.split("/").slice(0, 1);