2016-11-30 68 views
0

顯示DIV子()文本

var x= $('div.text').text(); 
 
var first15=x.substring(0, 55); 
 
$(".text").html(first15); 
 

 
if ($(".text").html(first15).length == 55) { 
 
    $('.ShowText').show(); 
 

 
}
.text 
 
{ 
 
    width:100px; 
 
    height:100px; 
 
    border:1px solid #000; 
 
    overflow: hidden; 
 
    font-size:10px; 
 
    position:relative; 
 
} 
 

 
.ShowText 
 
{ 
 
    display:none; 
 
    color:red; 
 
}
<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> 
 
<div class="ShowText">Show more...</div>

您好, 可以顯示()類= 「ShowText」 只有當文本有55個字符? 但是當我添加 「=」 始終顯示 「ShowText」

謝謝回答

+0

($(「。text」)。html(first15).length = 55) – PiotrS

回答

2

只用這一個

(first15.length == 55) 

因爲length

$(".text").html(first15).length 

這裏長度是jQuery的方法,將返回該元素中的元素數爲1,因此1不等於55。

+0

好的,謝謝你,這對我來說是完美的:)並感謝你的解釋 – PiotrS