使用JavaScript字符串()来创建更多链接 [英] Using javascript substring() to create a read more link
本文介绍了使用JavaScript字符串()来创建更多链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我开发了拉从数据库中一些内容并创建第一个100个字符后阅读全文链接如下一个传统的ASP页面;
&LT; DIV class=\"contentdetail\"><%=StripHTML(rspropertyresults.Fields.Item(\"ContentDetails\").Value)%></div>&LT;脚本类型=文/ JavaScript的&GT;
$(函数(){ VAR截止= 200;
变种文字= $('div.contentdetail')文本()。
VAR休息= $('div.contentdetail')文本()子(截止)。
如果(text.length&GT; 200){
VAR周期= rest.indexOf('。');
变种空间= rest.indexOf('');
截止+ = Math.max(Math.min(期间,空间),0);
} 。VAR visibleText = $('div.contentdetail')文本()子(0,截止); $('div.contentdetail')
html的(visibleText +('&LT;跨度&GT;'+其他+'&LT; / SPAN&GT;'))
.append('&LT;标题=阅读更多的风格=字体重量:大胆;显示:块;光标:指针;&GT;更多&放大器; hellip;&LT; / A&GT;')
。点击(函数(){
$(本).find('跨')切换();
$(本).find('A:去年')隐藏();
}); $('div.contentdetail跨度')隐藏()。
});
&LT; / SCRIPT&GT;
不过,此脚本明明只是后100个字符切文本关闭。 preferably我想它继续书写文字,直到第一个周期或空间,例如。这是可能的吗?
感谢您。
解决方案
VAR截止= 100;
变种文字= $('div.contentdetail')文本()。
VAR休息= text.substring(截止);
如果(text.length大于100){
VAR周期= rest.indexOf('。');
变种空间= rest.indexOf('');
截止+ = Math.max(Math.min(期间,空间),0);
}
//重新分配剩下的,因为我们重新计算截止
其余= text.substring(截止);
。VAR visibleText = $('div.contentdetail')文本()子(0,截止);
编辑:缩短了一点。
修改:修正了一个错误
I'm developing a Classic ASP page that pulls some content from a database and creates a Read more link after the first 100 characters as follows;
<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>
However, the script obviously just cuts the text off after 100 characters. Preferably I would like it to keep on writing text until the first period or space, for example. Is this possible to do?
Thank you.
解决方案
var cutoff = 100;
var text = $('div.contentdetail').text();
var rest = text.substring(cutoff);
if (text.length > 100) {
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);
EDIT: shortened it a bit. EDIT: Fixed a bug
这篇关于使用JavaScript字符串()来创建更多链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文