我試圖找出如何截斷第一段,我已經試過:子字符串截斷文本?
$div.children(('p:eq(0)').substring(0,100));
$div.children(('p:eq(0)'.substring(0,100)));
但無論工作過......
下面是完整代碼(在這裏它有人幫我!用)
$j('#hp-featured-item > div[id^="post-"]').each(function() {
var $div = $j(this),
$h2 = $div.find('h2:first'),
$obj = $div.find('object, embed, img').filter(':first'),
id = this.id.match(/^post-([0-9]+)$/);
if($obj.size() > 0){
// Find parent
var $par = $obj.closest('p');
// Move to top of div
$obj.prependTo($div);
// Remove the now empty parent
$par.remove();
if($obj.is('img')){
// You can't wrap objects and embeds with links, so make sure we just wrap images
$obj.wrap($j('<a></a>').attr('href', '/blog/?p='+id[1]));
}
}
// Wrap the contents of the h2, not the h2 itself, with a link
$h2.wrapInner($j('<a></a>').attr('href', '/blog/?p='+id[1]));
$div.children(('p:eq(0)').substring(0,100));
$div.children('p:gt(0)').remove();
});
我在這裏上傳了這段代碼(你甚至可以試試它):http://jsfiddle.net/Sk8erPeter/2NRFE/。我還在被截斷的文本上附加'[...]「。 – Sk8erPeter 2011-12-27 21:17:10