我能否使用jQuery在一定数量的字符后隐藏DIV中的文本?
到目前为止,我认为它应该像这样进行 - jQuery会循环遍历文本,直到它达到一定数量的字符。然后,它将从该位置到文本末尾包装一个DIV,然后可以使用hide()方法隐藏它。我不确定如何插入这个包装文本。
欢迎任何其他解决方案。
谢谢。
我能否使用jQuery在一定数量的字符后隐藏DIV中的文本?
到目前为止,我认为它应该像这样进行 - jQuery会循环遍历文本,直到它达到一定数量的字符。然后,它将从该位置到文本末尾包装一个DIV,然后可以使用hide()方法隐藏它。我不确定如何插入这个包装文本。
欢迎任何其他解决方案。
谢谢。
我认为这个目的可以比上面解释的方式更容易地实现
$(function(){
$(".title").each(function(i){
len=$(this).text().length;
if(len>10)
{
$(this).text($(this).text().substr(0,10)+'...');
}
});
});
上述 jQuery 代码将隐藏 div 文本,如果它超过10个字符,并在结尾处添加省略号来说明还有更多内容。
jQuery(function(){ jQuery(".rs_rs_name").each(function(i){ len=$(this).text().length; if(len>45) { jQuery(this).text($(this).text().substr(0,45)+'...'); } }); });
- RobBenz这将隐藏部分文本
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>untitled</title>
<style type="text/css" media="screen">
.hide{
display: none;
}
</style>
<script src="js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
$(function(){
var $elem = $('p'); // The element or elements with the text to hide
var $limit = 10; // The number of characters to show
var $str = $elem.html(); // Getting the text
var $strtemp = $str.substr(0,$limit); // Get the visible part of the string
$str = $strtemp + '<span class="hide">' + $str.substr($limit,$str.length) + '</span>'; // Recompose the string with the span tag wrapped around the hidden part of it
$elem.html($str); // Write the string to the DOM
})
</script>
</head>
<body>
<p>Some lenghty string goes here</p>
</body>
</html>
Jeff,我有同样的问题,想要把文本限制器添加到动态内容中,这是我的解决方案:
// TEXT CHARACTER LIMITER
$(document).ready(function() {
$.fn.textlimit = function()
{
return this.each(function()
{
var $elem = $(this); // Adding this allows u to apply this anywhere
var $limit = 22; // The number of characters to show
var $str = $elem.html(); // Getting the text
var $strtemp = $str.substr(0,$limit); // Get the visible part of the string
$str = $strtemp + '<span class="hide">' + $str.substr($limit,$str.length) + '</span> ...';
$elem.html($str);
})
};
});
// Call the text limiter above
$(document).ready(function() {
$('.someGenericClass .anotherClass').textlimit()
});
$.fn.textlimit = function(limit)
{
return this.each(function(index,val)
{
var $elem = $(this);
var $limit = limit;
var $strLngth = $(val).text().length; // Getting the text
if($strLngth > $limit)
{
$($elem).text($($elem).text().substr( 0, $limit )+ "...");
}
})
};
**how to use**
$("#DivId").textlimit(60);
我正在用这个答案替换我的原始答案......
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var limit = 20;
var chars = $("#myDiv").text();
if (chars.length > limit) {
var visiblePart = $("<span> "+ chars.substr(0, limit-1) +"</span>");
var dots = $("<span class='dots'>... </span>");
var hiddenPart = $("<span class='more'>"+ chars.substr(limit-1) +"</span>");
var readMore = $("<span class='read-more'>Read More</span>");
readMore.click(function() {
$(this).prev().remove(); // remove dots
$(this).next().show();
$(this).remove(); // remove 'read more'
});
$("#myDiv").empty()
.append(visiblePart)
.append(dots)
.append(readMore)
.append(hiddenPart);
}
});
</script>
<style type="text/css">
span.read-more { cursor: pointer; color: red; }
span.more { display: none; }
</style>
</head>
<body>
<div id="myDiv">Here are all<br/> my characters.<br/> I would like to limit them to 20.</div>
</body>
</html>
len
变量有点闲置 =) - BalusCHTML:
25 是最大长度。
<input type="text" name="Occupation" id="Occupation" value="" placeholder=""
onkeypress="limitKeypress(event,this.value, 25)" class="form-control" required>
Javascript:
function limitKeypress(event, value, maxLength) {
if (value !== undefined && value.toString().length >= maxLength) {
event.preventDefault();
}
}