请看下面的代码:CODE
此代码适用于jsfiddle.net,但不适用于本地js代码,此代码显示和隐藏的文本更少
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var maxheight=118;
var showText = "More";
var hideText = "Less";
$('.textContainer_Truncate').each(function () {
var text = $(this);
if (text.height() > maxheight){
text.css({ 'overflow': 'hidden','height': maxheight + 'px' });
var link = $('<a href="#">' + showText + '</a>');
var linkDiv = $('<div></div>');
linkDiv.append(link);
$(this).after(linkDiv);
link.click(function (event) {
event.preventDefault();
if (text.height() > maxheight) {
$(this).html(showText);
text.css('height', maxheight + 'px');
} else {
$(this).html(hideText);
text.css('height', 'auto');
}
});
}
});
});
</script>htm代码:
<div class="textContainer_Truncate">
<p>content<br>
hello<br>
hello<br>
hello<br>
hello<br>
hello<br>
hello<br>
hello<br>
hello<br>
hello<br>
hello<br>
hello<br>
hello<br>
hello<br>
hello<br>
hello<br>
hello<br>
</p>
</div>文件夹内容: index.htm和jquery-1.8.2.min.js如何在本地使用此代码?
发布于 2012-10-22 22:00:39
我试着复制你所做的,发现它工作得很好,没有错误。
考虑下载此解决方案(一个文件夹包含一个index.html页面以及jQuery1.8.2.js文件)。也许你做了一些额外的Test Project
更新:在复制你的代码时,我注意到一些东西,你的代码中有一个额外的问号,看看下面的图片

https://stackoverflow.com/questions/13012869
复制相似问题