[jQuery] Grab a bunch of HTML and exhibit an excerpt - help

[jQuery] Grab a bunch of HTML and exhibit an excerpt - help


Easy guys,
I've cobbled this script together to help me implement 'read more...'
links on this site I'm developing: http://www.blowcreative.co.uk/blog/
It does the job, but I lose all the mark-up in the process :[ It
doesn't cater for a bunch of tags like <img>, <ol>, <ul> etc. slice()
ain't being too kind to me.
// create a 'read more' link, and hide remaining text, if post content
exceeds quota
jQuery(function ($) {
// get total characters (including spaces) in content
$('.scrollable .scrollableItem').each(function () {
var totalContent = $(this).find('p').not('.wp-caption-
text').not('.postmetadata').text();
        // remove all post content, but not the page content (.entry p). we
will put the appropriate content back later
$(this).find('p').not('.entry p').not('.wp-caption-text').not
('.postmetadata').remove(); // fix: breaks without an image caption,
also needs support for multiple images and all other mce elements!
        // total characters allowed before cut off
var quota = 800;
// split totalContent into two parts
var arr = new Array(2);
arr[0] = totalContent.slice(0, [quota]);
arr[1] = totalContent.slice(quota, [totalContent.length]);
// set up post variables
var postTitle = $(this).parent().find('h2').text();
var postURL = $(this).parent().find('h2 a').attr('href');
// check if content exceeds quota. drop in appropriate content
and insert 'read more' link
if (totalContent.length > quota) {
// set up post variables
var postTitle = $(this).parent().find('h2').text();
var postURL = $(this).find('h2 a').attr('href');
// only display 1st part of content
$(this).find('.postmetadata').before('

' + arr[0] +
'<span class="readMore"> <a href=' + '"' + postURL + '"' + '>Read
more...</a></span>

'); // todo: would be better if orginal

's
were retained, atm these are omitted at the array stage
} else {
// display all content
$(this).find('.postmetadata').before('

' + totalContent
+ '

');
};
});
});
Any tips would be awesome.
Thanks,
L













































    • Topic Participants

    • mail