dou*_*ood 5 javascript jquery asp-classic
我正在开发一个经典ASP页面,它从数据库中提取一些内容,并在前100个字符后创建一个Read more链接,如下所示;
<div class="contentdetail"><%=StripHTML(rspropertyresults.Fields.Item("ContentDetails").Value)%></div>
<script type="text/javascript">
$(function() {
var cutoff = 200;
var text = $('div.contentdetail').text();
var rest = $('div.contentdetail').text().substring(cutoff);
if (text.length > 200) {
var period = rest.indexOf('.');
var space = rest.indexOf(' ');
cutoff += Math.max(Math.min(period, space), 0);
}
var visibleText = $('div.contentdetail').text().substring(0, cutoff);
$('div.contentdetail')
.html(visibleText + ('<span>' + rest + '</span>'))
.append('<a title="Read More" style="font-weight:bold;display: block; cursor: pointer;">Read More…</a>')
.click(function() {
$(this).find('span').toggle();
$(this).find('a:last').hide();
});
$('div.contentdetail span').hide();
});
</script>
Run Code Online (Sandbox Code Playgroud)
但是,脚本显然只是在100个字符后关闭文本.例如,我希望它继续写文本直到第一个句号或空格.这可能吗?
谢谢.
var cutoff = 100;
var text = $('div.contentdetail').text();
var rest = text.substring(cutoff);
if (text.length > cutoff) {
var period = rest.indexOf('.');
var space = rest.indexOf(' ');
cutoff += Math.max(Math.min(period, space), 0);
}
// Assign the rest again, because we recalculated the cutoff
rest = text.substring(cutoff);
var visibleText = $('div.contentdetail').text().substring(0, cutoff);
Run Code Online (Sandbox Code Playgroud)
编辑:稍微缩短一下。 编辑:修复了一个错误 编辑:生活质量改善
| 归档时间: |
|
| 查看次数: |
10565 次 |
| 最近记录: |