Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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_String - Fatal编程技术网

如何使用javascript截断字符串?

如何使用javascript截断字符串?,javascript,string,Javascript,String,我正在使用Jribble将来自Dribble的提要引入我的站点。我非常想把描述缩短为两行。这可能吗 <script type="text/javascript"> $(document).ready(function getDribbbleShots() { $.jribbble.getShotsByPlayerId('abenjamin765', function (playerShots) { var html = []; $.each(pla

我正在使用Jribble将来自Dribble的提要引入我的站点。我非常想把描述缩短为两行。这可能吗

<script type="text/javascript">
$(document).ready(function getDribbbleShots() {   
  $.jribbble.getShotsByPlayerId('abenjamin765', function (playerShots) {
      var html = [];

      $.each(playerShots.shots, function (i, shot) {
          html.push('<div class="col-md-4"><div class="thumbnail"><a href="' + shot.url + '" target="_blank">');
          html.push('<img class="shot-image" src="' + shot.image_url + '" ');
          html.push('alt="' + shot.title + '"></a><div class="caption"><h4>'+ shot.title +'</h4><p>'+shot.description+'</p></div></div></div>');
      });
      $('.dribbble-feed').html(html.join(''));
  }, {page: 1, per_page: 9});
});
</script>

通过限制css中描述的高度,并在css中设置文本溢出行为,可以隐藏超过某一点的文本

.ellipsis {
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
请注意,这仅适用于单行描述。如果您想将描述扩展到多行,有几种方法可以做到这一点

见:

或者,如果您不在乎溢出是否用省略号表示,您可以简单地设置

.thumbnail .caption p {
  height: 40px;
  overflow: hidden;
}

.split'\n'.slice0,2?我应该把它放在哪里?我对JavaScription不太熟悉,我应该把它放在哪里?@AaronBenjamin你可以在代码中用它代替shot.description。谢谢Luke。我仍然没有看到它被截断:这是因为您的描述不是多行的。您试图实现的不是截断字符串,而是隐藏超过某个点的文本。你完全可以在CSS中完成这项工作。我尝试使用jQuerySmartEllissis,但仍然没有成功。