显示所有文字的长度

时间:2016-11-30 15:59:28

标签: javascript html

$('.text').show().html().length="15";
.text
{
  width:100px;
  height:100px;
  border:1px solid #000;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="text">.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

</div>

您好, 是否可以只显示所有文本中的15个第一个字符?

5 个答案:

答案 0 :(得分:3)

使用html()text()方法进行回调,并使用String#slice方法获取长度为15的HTML字符串。

$('.text').show().html(function(_, oldHtml) {
  return oldHtml.slice(0, 15);
});
.text {
  width: 100px;
  height: 100px;
  border: 1px solid #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="text">. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to
  using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their
  infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

</div>

答案 1 :(得分:1)

您可以使用JavaScript函数substr查找前15个字符,如下所示:

var string=document.getElementById('paragraph').innerHTML;
var subs=string.substr(0,15);
document.getElementById('paragraph').innerHTML=subs;
<p id="paragraph">hello world. this is a string to only show the first 15 characters of a string.</p>

答案 2 :(得分:1)

你可以使用:

    var text=$(".text").html();
    var res =text.substr(0, 15);
    alert(res);

答案 3 :(得分:1)

&#13;
&#13;
var x= $('div.text').text();
var first15=x.substring(0, 15);
$(".text").html(first15);
&#13;
.text
{
  width:100px;
  height:100px;
  border:1px solid #000;
  overflow: hidden;
 }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="text">.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

</div>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

使用jquery读取和写入元素和正则表达式来分割字符串,可能已经提到了更好的解决方案,这只是另一个

var text = $('.text').text(); //get the text from the div
var splitted = text.match(/.{1,15}/g); //split the text into groups of 15
var first15 = splitted[0]; //get the first group of elements; 
$('.text').text(first15); //place the desired text there